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

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

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)

1 comment

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

1 answer

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

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