Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

local char = plr.Character or plr.CharacterAdded:Wait()

2 comments

0

Thanks so much I would've never though to do that.

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

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