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

The character in my line of code isnt working. it brings up errors in output?

Asked by 5 years ago

I don't know how to fix this problem in my scripting. I just made the animation stop when you release the Shift key and for some reason it says Character is nil value??? Im so confused...

The Character part in this line is whats causing the error.

local playAnim = Character.Humanoid:LoadAnimation("Anim")

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)
    if key:lower() == string.char(48) then
        local hum = game.Players.LocalPlayer.Character.Humanoid
        if hum then
            hum.WalkSpeed = 50
            local Anim = Instance.new("Animation")
            Anim.AnimationId = 'rbxassetid://2250415227'
            local playAnim = Character.Humanoid:LoadAnimation(Anim)
            playAnim:Play()
        end
    end
end)

mouse.KeyUp:connect(function(key)
    if key:lower() == string.char(48) then
        local hum = game.Players.LocalPlayer.Character.Humanoid
        if hum then
            hum.WalkSpeed = 20
            local Anim = Instance.new("Animation")
            Anim.AnimationId = 'rbxassetid://2250415227'
            local playAnim = Character.Humanoid:LoadAnimation("Anim")
            playAnim:Stop()
        end
    end
end)
0
You never defined character...there's no need to anyway, just do hum:LoadAnimation(Anim).........also, you should be using UserInputService, KeyDown is very old, outdated, and depriciated, https://www.robloxdev.com/code-sample/UserInputService-InputBegan Vulkarin 581 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Hi

Issue is you never defined Character

So to solve this it's pretty simple. Just define Character.

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key) -- avoid using :connect use :Connect
    if key:lower() == string.char(48) then
        local hum = Char.Humanoid
        if hum then
            hum.WalkSpeed = 50
            local Anim = Instance.new("Animation")
            Anim.AnimationId = 'rbxassetid://2250415227'
            local playAnim = Char.Humanoid:LoadAnimation(Anim) -- problem fixed
            playAnim:Play()
        end
    end
end)

mouse.KeyUp:Connect(function(key) -- avoid :connect
    if key:lower() == string.char(48) then
        local hum = Char.Humanoid -- use char variable
        if hum then
            hum.WalkSpeed = 20
            local Anim = Instance.new("Animation")
            Anim.AnimationId = 'rbxassetid://2250415227'
            local playAnim = Char.Humanoid:LoadAnimation("Anim") -- problem fixed
            playAnim:Stop()
        end
    end
end)

Hope this helped

Ad

Answer this question