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

attempt to index upvalue 'character' (a nil value)?

So i made a sprint script that has a sprinting animation play while pressing shift. It works fine in studio but when i took it to live game, i got the error in the Question title (on lines 7 and 17)

here is the code

local UIS = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local character = player.Character

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		character.Humanoid.WalkSpeed = 32
		local anim = Instance.new('Animation')
		anim.AnimationId = 'rbxassetid://01516599081'
		playAnim = character.Humanoid:LoadAnimation(anim)
		playAnim:Play()
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		character.Humanoid.WalkSpeed = 16
		playAnim:Stop()
	end
end)

Im not sure why it cant seem to find character? but in studio it does?

2 comments

0

try: repeat wait() until player.Character

0

i have already tried that, thank you though.

1 answer

Hello, I did some research and realized that it seems to be a scope issue?

I'm not completely sure why but i had to move

	local player = game.Players.LocalPlayer
	character = player.Character

into the first function so the new script reads fully like

UIS = game:GetService('UserInputService')

UIS.InputBegan:Connect(function(input)
	local player = game.Players.LocalPlayer
	character = player.Character
	if input.KeyCode == Enum.KeyCode.LeftShift then
		character.Humanoid.WalkSpeed = 40
		local anim = Instance.new('Animation')
		anim.AnimationId = 'rbxassetid://01516599081'
		playAnim = character.Humanoid:LoadAnimation(anim)
		playAnim:Play()
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		character.Humanoid.WalkSpeed = 16
		playAnim:Stop()
	end
end)

Although i am glad i fixed it, if anyone knows why this is, i would love to know. thanks!

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