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

Error:Humanioid is not a valid member of Model?

Asked by 4 years ago

Summary:

I have an animation script(also a sprint script) that will play once the player is pushing left shift. The problem is that it keeps giving me the error as as said in the title. After I added and idle animation script everything broke. I don't know if I even did this script right or if I should keep the animations separate with the sprint script. If you can help me it would be most appreciated.

Code:

local Player = game.Players.LocalPlayer
local Character = Player.Character

repeat wait() until game.Players.LocalPlayer

m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
    if key == "0" then 
        local run = Instance.new('Animation')
        run.AnimationId = 'rbxassetid://5013482632'
        runAnim = Character.Humanoid:LoadAnimation(run)
        runAnim:play()
        Character.Humanoid.WalkSpeed = 20
    end
end)

m.KeyUp:connect(function(key)
    if key == "0" then
        runAnim:stop()
        local walk = Instance.new('Animation')
        walk.AnimationId = 'rbxassetid://5013378526'
        local walkAnim = Character.Humanoid:LoadAnimation(walk)
        walkAnim:Play()
        Character.Humanoid.WalkSpeed = 8
    end
end)

Character.Humanoid.Running:Connect(function(speed)
    local Humanoid = Character:WaitForChild("Humanoid")
    if Character.Humanioid.WalkSpeed < 0.5 then --this is where it happens
        local idle = Instance.new('Animation')
        idle.AnimationId = 'rbxassetid://5022482368'
        local idleAnim = Character.Humanoid:LoadAnimaton(idle) -- sometimes it happens here too
        idleAnim:Play()
    end
end)

0
Sorry for replying late ill edit my script for those variables 123nabilben123 499 — 4y

2 answers

Log in to vote
2
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The only error I can see is that your referencing something that is nil / meaning it doesn't exist the error is that Humanoidis misspelled

Also, try adding a character wait in case the character hasn't been loaded. Try doing this:

local Player = game.Players.LocalPlayer
Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
repeat wait() until game.Players.LocalPlayer

m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
    if key == "0" then 
        local run = Instance.new('Animation')
        run.AnimationId = 'rbxassetid://5013482632'
        runAnim = Character.Humanoid:LoadAnimation(run)
        runAnim:play()
        Character.Humanoid.WalkSpeed = 20
    end
end)

m.KeyUp:connect(function(key)
    if key == "0" then
        runAnim:stop()
        local walk = Instance.new('Animation')
        walk.AnimationId = 'rbxassetid://5013378526'
        local walkAnim = Character.Humanoid:LoadAnimation(walk)
        walkAnim:Play()
        Character.Humanoid.WalkSpeed = 8
    end
end)

Character.Humanoid.Running:Connect(function(speed)
    local Humanoid = Character:WaitForChild("Humanoid")
    if Character.Humanoid.WalkSpeed < 0.5 then --this is where it happens
WalkAnim:Stop()
        local idle = Instance.new('Animation')
        idle.AnimationId = 'rbxassetid://5022482368'
        local idleAnim = Character.Humanoid:LoadAnimaton(idle) -- sometimes it happens here too
        idleAnim:Play()
    end
end)

0
Thank you for reaching to me! You have fixed my script but, I am also having trouble with the walk animation. The walk animation keeps playing even know i put 'walkAnim:Stop()' above 'local idle = Instance.new('Animation'). astroblox17 -11 — 4y
1
What line? JesseSong 3916 — 4y
0
32 astroblox17 -11 — 4y
1
Edited if you want it to do it constantly add a loop. JesseSong 3916 — 4y
View all comments (5 more)
0
Here is a link to my problem: https://gfycat.com/sorrowfulajarbluebottlejellyfish astroblox17 -11 — 4y
1
You said the animation even know i put walkAnimStop above local idle = instance.new ("animation") I dont see it? JesseSong 3916 — 4y
0
i wait i forgot to edit the script(i lost the script due to my computer crashing, sorry for the inconvenience) astroblox17 -11 — 4y
0
walkAnim is nil due to scope. Use a global or local variable in the first scope. Also have an if statement checking if walkAnim ~= nil. youtubemasterWOW 2741 — 4y
1
Please paste your full script so I can help you. JesseSong 3916 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

You are having a problem where the character doesn't load in completely and your script still executes even though the character is not existent. A simple fix is to wait till the character loads in. You can do this with LocalPlayer.CharacterAdded:Wait() which will pause every single thing in the script till the character loads.

It's just a simple fix, you can do this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- waiting for character to load in completely before making scripts break.


m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
    if key == "0" then 
        local run = Instance.new('Animation')
        run.AnimationId = 'rbxassetid://5013482632'
        runAnim = Character.Humanoid:LoadAnimation(run)
        runAnim:play()
        Character.Humanoid.WalkSpeed = 20
    end
end)

m.KeyUp:connect(function(key)
    if key == "0" then
        runAnim:stop()
        local walk = Instance.new('Animation')
        walk.AnimationId = 'rbxassetid://5013378526'
        local walkAnim = Character.Humanoid:LoadAnimation(walk)
        walkAnim:Play()
        Character.Humanoid.WalkSpeed = 8
    end
end)

Character.Humanoid.Running:Connect(function(speed)
    local Humanoid = Character:WaitForChild("Humanoid")
    if Character.Humanioid.WalkSpeed < 0.5 then --this is where it happens
        local idle = Instance.new('Animation')
        idle.AnimationId = 'rbxassetid://5022482368'
        local idleAnim = Character.Humanoid:LoadAnimaton(idle) -- sometimes it happens here too
        idleAnim:Play()
    end
end)

Basically if the character is nil (non-existent) then we will wait till the character loads in. But there is a flaw with this too. Whenever the character dies too, the script will reference the old character which is nil, (non existent because it died)

In order for that to work, we have to restate our variables as soon the character loads.

local Player = game.Players.LocalPlayer
local Character -- leave blank we will fill it later

-- Event for waiting character to load:
Player.CharacterAdded:Connect(function(NewCharacter)
    Character = NewCharacter
    Character:WaitForChild("Humanoid")
end)


m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
    if key == "0" then 
        local run = Instance.new('Animation')
        run.AnimationId = 'rbxassetid://5013482632'
        runAnim = Character.Humanoid:LoadAnimation(run)
        runAnim:play()
        Character.Humanoid.WalkSpeed = 20
    end
end)

m.KeyUp:connect(function(key)
    if key == "0" then
        runAnim:stop()
        local walk = Instance.new('Animation')
        walk.AnimationId = 'rbxassetid://5013378526'
        local walkAnim = Character.Humanoid:LoadAnimation(walk)
        walkAnim:Play()
        Character.Humanoid.WalkSpeed = 8
    end
end)

Character.Humanoid.Running:Connect(function(speed)
    local Humanoid = Character:WaitForChild("Humanoid")
    if Character.Humanioid.WalkSpeed < 0.5 then --this is where it happens
        local idle = Instance.new('Animation')
        idle.AnimationId = 'rbxassetid://5022482368'
        local idleAnim = Character.Humanoid:LoadAnimaton(idle) -- sometimes it happens here too
        idleAnim:Play()
    end
end)

There! Now whenever you reset the character, you will get a brand new reference to the new character and won't run into nil character problems. I also removed repeat wait() until game.Players.LocalPlayer since the LocalPlayer always loads in first which means you'll never have loading problems for the player.

You should also make some variables to the humanoid and such, and use UserInputService. You can tell me if you want this added too.

Edit: here is the cleaned script just like you asked

local Player = game.Players.LocalPlayer
local Character -- leave blank we will fill it later
local Humanoid -- also leave blank

-- Event for waiting character to load:
Player.CharacterAdded:Connect(function(NewCharacter)
    Character = NewCharacter
    Humanoid = Character:WaitForChild("Humanoid")
end)


m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
    if key == "0" then 
        local run = Instance.new('Animation')
        run.AnimationId = 'rbxassetid://5013482632'
        runAnim = Humanoid:LoadAnimation(run)
        runAnim:play()
        Humanoid.WalkSpeed = 20
    end
end)

m.KeyUp:connect(function(key)
    if key == "0" then
        runAnim:stop()
        local walk = Instance.new('Animation')
        walk.AnimationId = 'rbxassetid://5013378526'
        local walkAnim = Humanoid:LoadAnimation(walk)
        walkAnim:Play()
        Humanoid.WalkSpeed = 8
    end
end)

Character.Humanoid.Running:Connect(function(speed)
    if Humanioid.WalkSpeed < 0.5 then --this is where it happens
        local idle = Instance.new('Animation')
        idle.AnimationId = 'rbxassetid://5022482368'
        local idleAnim = Humanoid:LoadAnimaton(idle) -- sometimes it happens here too
        idleAnim:Play()
    end
end)
0
Hello, thanks for reaching to me. Yes, I would like those variables added. astroblox17 -11 — 4y
0
Sorry for replying late ill edit my script for those variables 123nabilben123 499 — 4y

Answer this question