Changing humanoids walk speed wont work when the player dies

I made a stun script, how it works is that when you get attacked, you almost cant move and cant jump.
The script works, until the player dies. The problem is that after the player dies, their walk speed and jumpower will stay at 16 and 50 even when stunned. Everything else will work but changing the walk speed and jump power.

do a characteradded if timer… set walkspeed to what you want

1 Like

Where is this localscript located? Maybe it needs to be in startercharacter

sounds like the script is only running once, then gets destroyed when they die, and doesn’t get recreated

It’s because the existing variable is now destroyed, use a player.CharacterAdded so when the player dies it runs the function again. Also local char = char.Character is a bad practice, instead use local char = char.Humanoid or player.CharacterAdded:Wait(). If you are confused how to use player.CharacterAdded here is the script.

local player = game:GetService("Player").LocalPlayer

local timer = 0

local statsfol = player:FindFirstChild("Stats")
    local endtime = statsfol:FindFirstChild("Endtime")
    local human = char:FindFirstChild("Humanoid")
    
    statsfol:FindFirstChild("Stunned").Changed:Connect(function()
        if statsfol:FindFirstChild("Stunned").Value == true do
            timer += 0.1
            if timer >= endtime.Value then
                timer = 0
                human.WalkSpeed = 16
                human.JumpPower = 50
                game:GetService("ReplicatedStorage").Events.StunEnd:FireServer()
            end
            print(timer)
            wait(.1)
        end
    end)

player.CharacterAdded:Connect(function(char)
    local statsfol = player:FindFirstChild("Stats")
    local endtime = statsfol:FindFirstChild("Endtime")
    local human = char:FindFirstChild("Humanoid")
    
    statsfol:FindFirstChild("Stunned").Changed:Connect(function()
        if statsfol:FindFirstChild("Stunned").Value == true do
            timer += 0.1
            if timer >= endtime.Value then
                timer = 0
                human.WalkSpeed = 16
                human.JumpPower = 50
                game:GetService("ReplicatedStorage").Events.StunEnd:FireServer()
            end
            print(timer)
            wait(.1)
        end
    end)
end)

Move the script to StarterCharacterScripts so that it’s executed each time the local player’s character respawns/reloads etc.