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

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

Asked by 6 years ago
Edited 6 years ago

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?

0
try: repeat wait() until player.Character theCJarmy7 1293 — 6y
0
i have already tried that, thank you though. PoePoeCannon 519 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

Ad

Answer this question