WalkSpeed Remaining the Same Value after Dying

Heya there! How are you doing?

Recently, I have been making a runner game and one of the things I want to accomplish is to have the WalkSpeed remain in the same value whenever a Player dies based on the event Humanoid.Died:

I took a look on the forum to see if I could find any related topics, but I couldn’t find any that were related to my goal, leading me to create this post!

I remembered trying to make the WalkSpeed the same by using Humanoid.HealthChanged but that did not work either… I suppose I should try using an IntValue/NumberValue that keeps stored in the Player character and always puts WalkSpeed = IntValue?

Let me know if you know how I can solve this puzzle!

Thank you!

3 Likes

In the video above, I used the experience “Rage Runner” for reference! :smile:

2 Likes

I suggest checking when the player dies, setting their walkspeed to a value, waiting for them to respawn, and then setting their speed to the old value after their character has loaded.

There is also a property under StarterPlayer that is a starting walkspeed.
image

2 Likes

Something like this?

image

local StarterPlayer = game.StarterPlayer

local StarterCharacterScripts = StarterPlayer.StarterCharacterScripts

local HumanoidConfiguration = StarterCharacterScripts.HumanoidConfiguration

local NumberValue = HumanoidConfiguration.Value

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

local Character = Player.Character

local Humanoid = Character:FindFirstChildOfClass("Humanoid")

Player.CharacterAdded:Connect(function()
	
	Humanoid.WalkSpeed = NumberValue
	
	StarterPlayer.CharacterWalkSpeed = NumberValue
		
	print(Humanoid.WalkSpeed)
	
	print(StarterPlayer.CharacterWalkSpeed)
	
end)

Humanoid.Died:Connect(function()

	print(Player.Name .. " has died")
	
	Humanoid.WalkSpeed = NumberValue
	
	print(Humanoid.WalkSpeed)

	print(StarterPlayer.CharacterWalkSpeed)
	
	Player.CharacterAppearanceLoaded:Connect(function()
		
		StarterPlayer.CharacterWalkSpeed = NumberValue
		
		print(Humanoid.WalkSpeed)

		print(StarterPlayer.CharacterWalkSpeed)
		
	end)

end)
2 Likes

might wanna change

local NumberValue = HumanoidConfiguration.Value

to

local NumberValue = HumanoidConfiguration.Value.Value
1 Like

Did what you said as I completely forgot about this factor, but it doesn’t seem to print or change after pressing Play on Roblox Studio neither if I die, rather than just stating that the user has died

1 Like

Oh yeah, that’s because it’s parented under StarterCharacterScripts.
Parent it under StarterPlayerScripts and try again.

1 Like

Did the modifications

image

local StarterPlayer = game.StarterPlayer

local StarterPlayerScripts = StarterPlayer.StarterPlayerScripts

local HumanoidConfiguration = StarterPlayerScripts.HumanoidConfiguration

local NumberValue = HumanoidConfiguration.Value.Value

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

local Character = Player.Character

local Humanoid = Character:WaitForChild("Humanoid")

Player.CharacterAdded:Connect(function()
	
	Humanoid.WalkSpeed = NumberValue
	
	StarterPlayer.CharacterWalkSpeed = NumberValue
		
	print(Humanoid.WalkSpeed)
	
	print(StarterPlayer.CharacterWalkSpeed)
	
end)

Humanoid.Died:Connect(function()

	print(Player.Name .. " has died")
	
	Humanoid.WalkSpeed = NumberValue
	
	print(Humanoid.WalkSpeed)

	print(StarterPlayer.CharacterWalkSpeed)
	
	Player.CharacterAppearanceLoaded:Connect(function()
		
		StarterPlayer.CharacterWalkSpeed = NumberValue
		
		print(Humanoid.WalkSpeed)

		print(StarterPlayer.CharacterWalkSpeed)
		
	end)

end)

However, now it won’t find the Humanoid, and further errors keep appearing if I also switch to Players.PlayerAdded:Connect(function()

image

1 Like

Okay so I noticed a couple of things.

  1. You should probably use script.Parent instead of StarterPlayer.StarterPlayerScripts.
  2. It tries to call the players character before it loads, to fix that I suggest putting
Player.CharacterAdded:Wait()

local Character = Player.Character
  1. It never changes the character nor the humanoid to the new one when the player dies. Put this in the function that runs when the player’s character is added
Character = Player.Character
Humanoid = Character:WaitForChild("Humanoid")
1 Like

To fix that error add a task.wait() after you define the humanoid.

1 Like

Here’s the script with all the changes I provided.
I also deleted a bit of unneeded code. I have tested this one and it seems to work fine.

local HumanoidConfiguration = script.Parent

local NumberValue = HumanoidConfiguration.Value.Value

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

Player.CharacterAdded:Wait()

task.wait()

local Character = Player.Character

local Humanoid = Character:FindFirstChildOfClass("Humanoid")

Player.CharacterAdded:Connect(function()
	
	task.wait()
	
	Character = Player.Character

	Humanoid = Player.Character:FindFirstChildWhichIsA("Humanoid")
	
	task.wait(0.1)

	Humanoid.WalkSpeed = NumberValue

	print(Humanoid.WalkSpeed)

end)

Humanoid.Died:Connect(function()

	print(Player.Name .. " has died")

	NumberValue = Humanoid.WalkSpeed

	print(Humanoid.WalkSpeed)
	
end)
2 Likes

I copy and pasted but only gives the player the WalkSpeed as 16 by default instead of 50 from the Value

Hm, what’s being printed in the output?

1 Like

Hello, here’s a simpler script that works (Parent it under StarterPlayerScripts):

local StarterPlayer = game:GetService("StarterPlayer")

local player = game.Players.LocalPlayer
local walkSpeed = StarterPlayer.CharacterWalkSpeed

local function onCharacterAdded(character: Model)
    local humanoid : Humanoid = character:WaitForChild("Humanoid")
    humanoid.WalkSpeed = walkSpeed

    humanoid.Died:Once(function()
        walkSpeed = humanoid.WalkSpeed
    end)
end

player.CharacterAdded:Connect(onCharacterAdded)

image

Only display this

That’s the same script as the one I mentioned earlier right? It saves the player’s previous speed and sets it to their new character. It looks like you didnt change the speed.

If you want to make the player start with a certain speed change the property in studio as I first mentioned.

1 Like

It is the one you made for me!

I am going to see if it works setting the CharacterWalkSpeed as 50

It worked @Overseerzed ! Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.