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

Attempt to index global 'ChosenPlr' a nil value?

Asked by
bloxxyz 274 Moderation Voter
10 years ago

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

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

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(...)]

0
So, are you saying I should remove the ChosenPlr line completely? bloxxyz 274 — 10y
0
Or don't index the table in RandomPlr. 1waffle1 2908 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
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)]
0
Okay, I did this, I am getting the same error again. Workspace.Script:10: attempt to index global 'ChosenPlr' (a nil value) bloxxyz 274 — 10y

Answer this question