"if" is working, but "else" isn't.


AddStickTool("Restore Health", {
	Description = "Restores the target to full health.",
	Icon = "icon16/heart_add.png",
	CanTarget = targetply,
	OnRun = function(Player, Trace)
		local ply = Trace.Entity
		if ply:IsPlayer() then
			ply:SetHealth(ply:GetMaxHealth())
			Stick_SendChat(Player, "Restored " .. tostring(ply) .. " to full health.")
			Stick_SendChat(ply, "You have been restored to full health.")
		else
                        print("foobar")
		end
	end
})

The if is working perfectly fine, and it restores bots. However, when I aim it at the ground or anywhere else, the else doesn’t work. It doesn’t print. No errors.

wat.

Don’t forget the lua errors that you’re getting. Take a look at those.

Hint: IsValid

There isn’t any, odd, I know.

Really? Nothing to the effect of: “Attempt to index local ‘ply’ (a nil value)”?

Did you look in the server console?

Yep, nothing. http://prntscr.com/ara6tw


elseif not ply:IsValid() then

seems to have the exact same result.

Try this in the if:

[lua]if IsValid(ply) and ply:IsPlayer() then[/lua]

The rest can be the same.

I’m still 100% certain that you should be getting lua errors. Are you sure the function is even running?

That’s what I thought, too.

Same result. Yes I am sure it is running because when used on a player, it works fine. There are no errors in my server or client console.

I have to go now, but when I get home I will record a video to show it not working.

[editline]12th April 2016[/editline]

Decided to do it now:

[video]https://youtu.be/LpiuKGk950s[/video]

maybe check that you have an entity first

Huh?

Technically the world is an entity, the only reason I’d need to do that is if I was determining the map from the sky.

My guess is that whatever you’re using doesn’t call OnRun if the stick didn’t hit anything.

Then explain how this works fine ;o


AddStickTool("Roll", {
	Description = "Roll, 0-100",
	Icon = "icon16/sport_8ball.png",
	CanTarget = anything,
	OnRun = function(Player, Trace)
		local ply = Trace.Entity
		local roll = math.random( 0, 100 )
		if ply:IsPlayer() then
			Stick_SendChat(ply, "Roll: " .. roll)
			Stick_SendChat(Player, "Roll(" .. ply:Nick() .. "): " .. roll)
		else
			Stick_SendChat(Player, "Roll: " .. roll)
		end
	end
})

That works perfectly, but the else this:


AddStickTool("Restore Health", {
	Description = "Restores the target to full health.",
	Icon = "icon16/heart_add.png",
	CanTarget = targetply,
	OnRun = function(Player, Trace)
		local ply = Trace.Entity
		if IsValid(ply) and ply:IsPlayer() then
			ply:SetHealth(ply:GetMaxHealth())
			Stick_SendChat(Player, "debug")
			Stick_SendChat(Player, "Restored " .. tostring(ply) .. " to full health.")
			Stick_SendChat(ply, "You have been restored to full health.")
		else
			Player:SetHealth(Player:GetMaxHealth())
			Stick_SendChat(Player, "You have been restored to full health.")
		end
	end
})

does not. I am so confused, are they not the exact same thing?

The obvious question now is: what is AddStickTool and who made it? And where is the source code for it?

change CanTarget to anything (compare the two code blocks above)

[editline]12th April 2016[/editline]

Thanks, I’m an idiot.