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

Attempt to index local 'player' (a nil value) error?

Asked by 4 years ago
Edited 4 years ago

I'm trying to make it so that when the button is clicked, it checks if the text typed in was correct, and then creates a hint saying that said team wins. Here is my code: (this is inside of a TextButton gui btw)

gui = script.Parent.Parent
button = script.Parent

function onClicked(player)
    if script.Parent.Parent.box.Text == "Winner" or "WINNER" then
        local hint = Instance.new("Hint", workspace)
        hint.Text = player.Team.Team .. " wins!"
        gui.Enabled = false
        wait(5)
        hint:Destroy()
    end
end

script.Parent.MouseButton1Click:connect(onClicked)

Output: Players.Brad_xv.PlayerGui.Challenge2.button.Script:5: attempt to index local 'player' (a nil value)

1 answer

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
gui = script.Parent.Parent
button = script.Parent

function onClicked()
local player = game.Players.LocalPlayer
    if script.Parent.Parent.box.Text == "Winner" or "WINNER" then
        local hint = Instance.new("Hint", workspace)
        hint.Text = player.Team.Team .. " wins!"
        gui.Enabled = false
        wait(5)
        hint:Destroy()
    end
end

script.Parent.MouseButton1Click:connect(onClicked)
0
This is inside a script so instead of game.Players.LocalPlayer, is gui.Parent.Parent. 9mze 193 — 4y
Ad

Answer this question