Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

'if' statement doesn't execute in this script?

I don't know why it's not working. In the output, I get no problems though.

function onTouch(plr)
	player = game.Players:GetPlayerFromCharacter(plr.Parent)
	if player.PlayerGui.End.Value.Value == 0 then -- this doesn't execute
		player.PlayerGui.End.Value.Value = player.PlayerGui.End.Value.Value + 1
		player.PlayerGui.ScreenGui.Script.Disabled = false
		player.PlayerGui.ScreenGui.Frame.Visible = true
		player.PlayerGui.ScreenGui.ImageLabel.Visible = true
		player.PlayerGui.End:Play()
end
end

script.Parent.Touched:connect(onTouch)

-- Don't look at lines (4 to 8) there's nothing wrong with them. Just look at line 3, (the 'if' statement).

1 answer

✓ Accepted answer

First, 'plr' is really a bad thing to name that parameter, since it's not a player. It's the part that hit the brick that has the Touched event connect to it. Therefore it is most commonly named part, hit, or even hitPart.

You also should check if player exists on line 02, because if an NPC or another part touched this brick, it will result in an error. GetPlayerFromCharacter() either returns the player, or nil. On the case that it returns nil, you don't want your script to break.

If the if statement does not execute, than that's obviously because the value does not equal 0.

function onTouch(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		if player.PlayerGui.End.Value.Value == 0 then
			 player.PlayerGui.End.Value.Value = player.PlayerGui.End.Value.Value + 1
		        player.PlayerGui.ScreenGui.Script.Disabled = false
		        player.PlayerGui.ScreenGui.Frame.Visible = true
		        player.PlayerGui.ScreenGui.ImageLabel.Visible = true
		        player.PlayerGui.End:Play()
		end
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.