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

local Plr = game.Players.LocalPlayer

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

local Tool = script.Parent;


enabled = true




function onActivated()
    if not enabled  then
        return
    end

    enabled = false
    Tool.GripForward = Vector3.new(-0, -0.836, -0.549)
    Tool.GripPos = Vector3.new(1.4, -0.4, 0.3)
    Tool.GripRight = Vector3.new(0.996, -0.0509, 0.0776)
    Tool.GripUp = Vector3.new(0.0928, 0.547, -0.832)


    Tool.Handle.DrinkSound:Play()
    Humanoid.WalkSpeed = Humanoid.WalkSpeed +.5
    game.Players.LocalPlayer.leaderstats.Speed.Value = game.Players.LocalPlayer.leaderstats.Speed.Value +3

    wait(3)

    local h = Tool.Parent:FindFirstChild("Humanoid")
    if (h ~= nil) then
        if (Tool.Parent:FindFirstChild("SpeedEffect") == nil) then
            local s = script.Parent.SpeedEffect:Clone()
            s.Disabled = false
            s.Parent = Tool.Parent          
        end
    end


    Tool.GripForward = Vector3.new(-.976,0,-0.217)
    Tool.GripPos = Vector3.new(0.2,-.1,0)
    Tool.GripRight = Vector3.new(.217,0,-.976)
    Tool.GripUp = Vector3.new(0,1,0)


    wait(1)
    enabled = true

end



script.Parent.Activated:connect(onActivated)


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

local 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...

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

Try...

local chr = Plr.Character
if chr == nil then
  repeat
   -- do nothing.
  until chr ~= nil or chr == true
  -- This repeat loop will keep running until the character is valid. It will then continue the script.

end
local 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