Changing WalkSpeed when character respawns

I have been struggling with changing the walkspeed of a players humanoid upon them respawning after dying, i get no errors while testing - I am using RemoteEvent, i am not sure if that is neccesary

LocalScript is in the StarterCharacterScripts folder

-- LocalScript


local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")


local function onDeath()
	game.ReplicatedStorage.AddStats:FireServer()
end

hum.Died:Connect(onDeath)


-- Script


game.ReplicatedStorage.AddStats.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char:WaitForChild("Humanoid")
	local gui = plr.PlayerGui:WaitForChild("ValueGui")
	local textLabel = gui.JumpValue
	
	local walkSpeed = hum.WalkSpeed
	local jumpPower = hum.JumpPower
	print("Stats saved!")
	
	plr.CharacterAdded:Connect(function()
		print("character respawned")
		char:WaitForChild("Humanoid").WalkSpeed = walkSpeed
		hum.JumpPower = jumpPower
	end)
end)
2 Likes

In the LocalScript, you can put this, it is way shorter and it works.

local player = script.Parent

player.Humanoid.WalkSpeed = 16 
-- ^ Change this to the walking speed you want, 16 is the default
player.Humanoid.JumpPower = 50 
-- ^ Change this to the jump power you want, 50 is the default

You don’t need to use RemoteEvents or server sided scripts to do this.
Hope this helped you.

My bad, i didnt describe it well enough. The idea of the game is that your walkspeed increases every few seconds and so does your JumpPower and i want to store the players speed and jump when they die so i can give them the exact amount of speed and jump they had before they died.

Oh, in that case, I don’t know how to help. I’m sorry :confused:

In a server script in ServerScriptService:

local defaultWalkSpeed = 16
local defaultJumpPower = 50

game.Players.PlayerAdded:Connect(function(plr)
    local walkSpeedValue = Instance.new(“IntValue”)
    walkSpeedValue.Name = “WalkSpeedValue”
    walkSpeedValue.Value = defaultWalkSpeed
    local jumpPowerValue = Instance.new(“IntValue”)
    jumpPowerValue.Name = “JumpPowerValue”
    jumpPowerValue.Value = defaultJumpPower
    walkSpeedValue.Parent = plr
    jumpPowerValue.Parent = plr
    plr.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild(“Humanoid”, 5)
        if humanoid then
            humanoid.WalkSpeed = walkSpeedValue.Value
            humanoid.JumpPower = jumpPowerValue.Value
            humanoid.Changed:Connect(function()
                walkSpeedValue.Value = humanoid.WalkSpeed
                jumpPowerValue.Value = humanoid.JumpPower
            end)
        end
    end)
end)

I was unable to test this as I am on a phone, but it should work. If you want it to work specifically with JumpHeight instead of JumpPower, just go into the script editor and replace all instances of “JumpPower” with “JumpHeight”. Let me know if you get any errors.

I tested it out, it did not work and did not mention any errors

Can I see your entire local script?

The script in the post is the entire script. I have most of my code in different scripts

Whoops sorry wrong thread, but since I’m here. Do the print statements fire?

Yes they do and when i print in between the last few lines it works too

Like this

Looks like all of the code is running its just not adding the speed and jump

plr.CharacterAdded:Connect(function()
		print("character respawned")
		char:WaitForChild("Humanoid").WalkSpeed = walkSpeed
		print("1")
		hum.JumpPower = jumpPower
		print("2")

Check my revised script, I forgot to add “.Parent” to the jumpPowerValue

Yeah i saw that i also ran the script with the .Parent it did not seem to work

You are trying to reference “char”, which was destroyed when you respawned.

The edited section of the script would be something like this:

plr.CharacterAdded:Connect(function(newChar)
	print("character respawned")
	local newHum = newChar:WaitForChild(“Humanoid”)
    newHum.WalkSpeed = walkSpeed
    newHum.JumpPower = jumpPower
end)

FYI, you technically only need a server script for something like this to work instead of firing remotes.

I got help from another website and found the solution.

This is the script

local Players = game:GetService("Players") 
local plr = game.Players.PlayerAdded:Wait()

local function CharacterAdded(Character) -- Character has respawned
	print("character respawned") 
	local Humanoid = Character:WaitForChild("Humanoid")

	Humanoid.JumpPower = plr.Values.JumpPower.Value -- Setting the JumpPower to the value of the IntValue
	Humanoid.WalkSpeed = plr.Values.WalkSpeed.Value -- same here just with WalkSpeed


	local function JumpPowerChanged()
		plr.Values.JumpPower.Value = Humanoid.JumpPower -- Saving the JumpPower onto the Value everytime the player gets more JumpPower
	end
	

	local function WalkSpeedChanged()
		plr.Values.WalkSpeed.Value = Humanoid.WalkSpeed -- same here just with WalkSpeed
	end
	
	Humanoid:GetPropertyChangedSignal("JumpPower"):Connect(JumpPowerChanged) -- Listening for changes to the JumpPower then running the function above ^
	Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(WalkSpeedChanged)

end
	
	plr.CharacterAdded:Connect(CharacterAdded)