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?
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!