Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
RAYAN1565 691 Moderation Voter
10 years ago

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

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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
Ad

Answer this question