Problem with a small lua script

Status
Not open for further replies.

Xkower8181

Member
Hello this me and this time I have a Lua problem
So here's a code
Code:
addHook("MobjMoveCollide", function (npc, thing)
        if npc.target.type == MT_PLAYER
        and thing.valid and thing.health
        and abs(npc.z - thing.z) < thing.height then
                local player = npc.target.player
                if thing.flags & MF_MONITOR then
                        P_KillMobj(thing, npc, player.mo)
                elseif thing.flags & (MF_ENEMY|MF_BOSS) then
                        P_DamageMobj(thing, npc, player.mo, 1)
                    end
                end
            end      
        end
    end
end, MT_NPC)
And here a screenshot of an error
VMWS3Bl.png
 
Your code has too many "end"s. You need one to close the inner if loop, one to close the outer if loop and one to close the function, but that should be it.
 
Your code has too many "end"s. You need one to close the inner if loop, one to close the outer if loop and one to close the function, but that should be it.
Thanks but now it shows this error
xypIUSt.png

and here's an edited code
Code:
addHook("MobjMoveCollide", function (npc, thing)
	if npc.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc.z - thing.z) < thing.height then
		local player = npc.target.player
		if thing.flags & MF_MONITOR then
			P_KillMobj(thing, npc, player.mo)
		elseif thing.flags & (MF_ENEMY|MF_BOSS) then
			P_DamageMobj(thing,  npc, player.mo, 1)
end, MT_NPC)

EDIT: Guys on SRB2Fun helped me so please lock this topic
 
Last edited:
HOLD IT!
I've got new problem with new code but still kinda about same thing
Code:
addHook("MobjMoveCollide", function (npc, thing)
        if npc.state == S_NPCJUMPS2
        elseif npc.state == S_NPCJUMP1
        elseif npc.state == S_NPCJUMP2
        elseif npc.state == S_NPCJUMP3
        elseif npc.state == S_NPCJUMP4
	if npc.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc.z - thing.z) < thing.height then
		local player = npc.target.player
		if thing.flags & MF_MONITOR then
		    P_KillMobj(thing, npc, player.mo)
		elseif thing.flags & (MF_ENEMY|MF_BOSS) then
		    P_DamageMobj(thing, npc, player.mo, 1)
              end
         end
     end
end, MT_NPC)

addHook("MobjMoveCollide", function (npc2, thing)
	if npc2.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc2.z - thing.z) < thing.height then
		local player = npc2.target.player
		if thing.flags & MF_SPECIAL then
                if not thing.flags == (MF_ENEMY|MF_BOSS|MF_PUSHABLE|MF_SPRING|MF_BOUNCE|MF_SHOOTABLE)
		    P_KillMobj(thing, npc2, player.mo)
            end
        end
    end
end, MT_NPC)
and like always screenshot of problem
7AyyZP5.png

Edit: I deciceded to add a reason when that happens
So i happens when i try to interact a ring and npc object
 
Last edited:
What I think is happening is the game is checking if the player has collided with ANY object, not just enemies, or the enemy/npc in question here. Now don't quote me on this, but I believe you're gonna have to exclude the enemies and rings in the code.
 
First of all...

Code:
addHook("MobjMoveCollide", function (npc, thing)
        if npc.state == S_NPCJUMPS2
        elseif npc.state == S_NPCJUMP1
        elseif npc.state == S_NPCJUMP2
        elseif npc.state == S_NPCJUMP3
        elseif npc.state == S_NPCJUMP4
The hell is this?! I genuinely can't tell if you wanted to use "or" or something, it's carnage.

Code:
if npc.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc.z - thing.z) < thing.height then
		local player = npc.target.player
		if thing.flags & MF_MONITOR then
		    P_KillMobj(thing, npc, player.mo)
		elseif thing.flags & (MF_ENEMY|MF_BOSS) then
		    P_DamageMobj(thing, npc, player.mo, 1)
              end
         end
     end
end, MT_NPC)

addHook("MobjMoveCollide", function (npc2, thing)
	if npc2.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc2.z - thing.z) < thing.height then
		local player = npc2.target.player
		if thing.flags & MF_SPECIAL then
                if not thing.flags == (MF_ENEMY|MF_BOSS|MF_PUSHABLE|MF_SPRING|MF_BOUNCE|MF_SHOOTABLE)
		    P_KillMobj(thing, npc2, player.mo)
            end
        end
    end
end, MT_NPC)

"if npc2.target.type == MT_PLAYER"

Do you KNOW if npc2 has a target? Is npc2.target valid? You never check that.

"if npc2.target and npc2.target.valid"
put your code INSIDE this check, and for God's sake don't forget your "end"s anywhere.

(Do the same for the other function too, you do the exact same mistake)
 
Last edited:
First of all...

Code:
addHook("MobjMoveCollide", function (npc, thing)
        if npc.state == S_NPCJUMPS2
        elseif npc.state == S_NPCJUMP1
        elseif npc.state == S_NPCJUMP2
        elseif npc.state == S_NPCJUMP3
        elseif npc.state == S_NPCJUMP4
The hell is this?! I genuinely can't tell if you wanted to use "or" or something, it's carnage.

Code:
if npc.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc.z - thing.z) < thing.height then
		local player = npc.target.player
		if thing.flags & MF_MONITOR then
		    P_KillMobj(thing, npc, player.mo)
		elseif thing.flags & (MF_ENEMY|MF_BOSS) then
		    P_DamageMobj(thing, npc, player.mo, 1)
              end
         end
     end
end, MT_NPC)

addHook("MobjMoveCollide", function (npc2, thing)
	if npc2.target.type == MT_PLAYER
	and thing.valid and thing.health
	and abs(npc2.z - thing.z) < thing.height then
		local player = npc2.target.player
		if thing.flags & MF_SPECIAL then
                if not thing.flags == (MF_ENEMY|MF_BOSS|MF_PUSHABLE|MF_SPRING|MF_BOUNCE|MF_SHOOTABLE)
		    P_KillMobj(thing, npc2, player.mo)
            end
        end
    end
end, MT_NPC)

"if npc2.target.type == MT_PLAYER"

Do you KNOW if npc2 has a target? Is npc2.target valid? You never check that.

"if npc2.target and npc2.target.valid"
put your code INSIDE this check, and for God's sake don't forget your "end"s anywhere.

(Do the same for the other function too, you do the exact same mistake)

Thanks Zipper now you can really close this thread
 
Status
Not open for further replies.

Who is viewing this thread (Total: 0, Members: 0, Guests: 0)

Back
Top