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

Attempt to index upvalue 'char' (a nil value)?

Asked by
kfish98 16
7 years ago
Edited 7 years ago

So, I've been experimenting a lot with KeyPressing as well as animation and it has been working out (in studio only). I would look for errors in the local logs in the actual game and would get "Attempt to index upvalue 'char' (a nil value)".

So here's the code:

local Player = script.Parent.Parent
local mouse = Player:GetMouse()
local char = Player.Character


function onKeyDown(key)
    key = key:lower()
    if key == "r" then
--------------------------------------------------------------------------      
            local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=676773078"

char.Humanoid.WalkSpeed = 0     

local animTrack = char.Humanoid:LoadAnimation(animation)

wait(0.5)
animTrack:Play()
---------------------------------------------------------------------------


        wait(2.5)
char.Humanoid.WalkSpeed = 16

end
    end
mouse.KeyDown:connect(onKeyDown)

Now, this is in a LocalScript which apparently brings more complications like being unable to run things globally? Like, if I changed local char to just char, it doesn't tag char because it's global? I don't know, I'm not very adept at this.

1 answer

Log in to vote
2
Answered by 7 years ago

You're not waiting for the character to load. You can do so using this method:

local char = plr.Character or plr.CharacterAdded:Wait()
0
Thanks so much I would've never though to do that. kfish98 16 — 7y
1
I second that this is the issue, what's confusing to me is why Lua would refer to `char` as an upvalue in this case... adark 5487 — 7y
Ad

Answer this question