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

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

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

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)

1 comment

0

This is inside a script so instead of game.Players.LocalPlayer, is gui.Parent.Parent.

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