How to change the player's speed and health when he join in the game?

I made a script so that when the player enters the game he has a certain speed and health

 plr.CharacterAdded:Connect(function(char)
			
			local hum = char:WaitForChild("Humanoid")
			hum.WalkSpeed =90
			hum.MaxHealth = 100
		end)
end`

but when I enter the game it continues at the default speed and yes, I’ve tried looking at some similar cases but I only found one and it didn’t work

Waiting for Answers

I added game.Players.PlayerAdded:Connect(function(plr) to the code and it seems to be working.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		hum.WalkSpeed = 90
		hum.MaxHealth = 100
	end)
end)

Try checking if the player’s character already exists too, because the function might run after the player’s character was created.

if plr.Character then
	local char = plr.Character

	local hum = char:WaitForChild("Humanoid")
	hum.WalkSpeed = 90
	hum.MaxHealth = 100
end

I also included this, I just cut it to the one that had the problem

I executed the code but it didn’t work

You can change the CharacterWalkSpeed value in the Properties of StarterPlayer.

As for the health, make a script in StarterCharacterScripts.

local default_health = 1

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Health = default_health
Humanoid.MaxHealth = default_health

Apologies. Changing the value doesn’t change the WalkSpeed.

This should work - place in a normal script inside of StarterCharacterScript.

local default_health = 1
local default_walkspeed = 1

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Health = default_health
Humanoid.MaxHealth = default_health

script.Parent.Parent:FindFirstChild(Humanoid)
Humanoid.WalkSpeed = 0
1 Like

@Rubin541 's code should work. Are you sure you are executing this code in a server script?

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