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

attempt to index field 'Character' (a nil value)?

Asked by 6 years ago
Edited 6 years ago

My drink script gives me the error on the title when I use it out of Roblox Studio. Please help me fix it. Here is the script: It says the error is on line 3

01local Plr = game.Players.LocalPlayer
02 
03local Humanoid = Plr.Character:WaitForChild("Humanoid")
04 
05local Tool = script.Parent;
06 
07 
08enabled = true
09 
10 
11 
12 
13function onActivated()
14    if not enabled  then
15        return
View all 54 lines...

2 answers

Log in to vote
1
Answered by
thesit123 509 Moderation Voter
6 years ago

Character of a player isn't supposed to be managed as a child of player, but a property.

Between line 1 and 3 just add

1local char = plr.Character or plr.CharacterAdded:wait()

it always seem to work for me :)

0
good answer. User#19524 175 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

For some reason, the Character property bugs many scripts.

Instead of...

1local Humanoid = Plr.Character:WaitForChild("Humanoid")

Try...

1local chr = Plr.Character
2if chr == nil then
3  repeat
4   -- do nothing.
5  until chr ~= nil or chr == true
6  -- This repeat loop will keep running until the character is valid. It will then continue the script.
7 
8end
9local Humanoid = Plr.Character:FindFirstChild("Humanoid") -- The Humanoid is packed with the Character model already.

I haven't tested it. It's Midnight here in the Philippines so chances are it might fail.

Hoped I helped if it worked otherwise.

0
I might be availanle tomorrow after school. Anyways Good night from the Philippines. Bilinearly 58 — 6y
0
You wouldn't want to 'do nothing' inside of the repeat. chr is only being set to character once, so if it's nil initially, that's an infinite loop, so you would want do continuously set chr = Plr.Character until it's not nil. climethestair 1663 — 6y
0
Why would you check if chr equals true User#19524 175 — 6y
0
Can I get code like the full script with this in it. What I did with this is not working. Same error. appleprogamer_59 29 — 6y

Answer this question