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 global 'ChosenPlr' a nil value?

It shouldn't be nil, I have the variable in my script, but it will not identify. Here is the script, it's pretty short, if someone could help that would be great. The error is on line 11, I commented the error point.

h = Instance.new("Hint", game.Workspace)
plr = game.Players:GetPlayers()
game.Players.PlayerAdded:connect(function(players)
    wait(5)
    h.Text = ("Welcome to Game")
    wait(2)        
    h.Text = ("Choosing Ghost..")
    RandomPlr = plr[math.random(1, game.Players.NumPlayers)]
    ChosenPlr = plr[RandomPlr] --ChosenPlr variable identified right here
    wait(3)
    h.Text = ("The ghost is:") .. ChosenPlr.Name --This line, I get "error 'ChosenPlr' nil value" ?
end)

Thank you.

2 answers

You're already getting the chosen player in RandomPlr by indexing the plr table. ChosenPlr is getting plr[plr[math.random(...)]] instead of plr[math.random(...)]

2 comments

0

So, are you saying I should remove the ChosenPlr line completely?

0

Or don't index the table in RandomPlr.

RandomPlr = plr[math.random(1, game.Players.NumPlayers)]
ChosenPlr = plr[RandomPlr] --ChosenPlr variable identified right here

You are getting the player and then getting it a second time using the player as the arguement.

Get rid of RandomPlr and simply do this:

ChosenPlr = plr[math.random(1, game.Players.NumPlayers)]

1 comment

0

Okay, I did this, I am getting the same error again. Workspace.Script:10: attempt to index global 'ChosenPlr' (a nil value)

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