Player speed is not setting the correct walkspeed i want

Making a stats GUI that if you have enough points, you can increase your speed stat by +1, increasing the player’s walkspeed by +1, eachtime it increases it’s stored inside an IntValue.

I had a problem where the player’s IntValue reset whenever the player dies, but i realized the IntValue doesn’t reset inside PlayerGui, so i changed my join script:

local function PlayerAdded(players)
players.CharacterAdded:Connect(function(char)
	char.Humanoid.WalkSpeed = players.PlayerGui:WaitForChild("Stats").Buffs.Speed.Value
	print("speed set")
	char.Humanoid.JumpPower = 5
	char.Humanoid.MaxHealth = 10
	char.RightUpperArm.Size = Vector3.new(1, 0.1, 1)
	char.LeftUpperArm.Size = Vector3.new(1, 0.1, 1)
	char.RightLowerArm.Transparency = 1
	char.LeftLowerArm.Transparency = 1
	char.RightHand.Transparency = 1
	char.LeftHand.Transparency = 1
end)

end

game.Players.PlayerAdded:Connect(PlayerAdded)

So the player’s speed will set to be the same as the IntValue’s value whenever they respawn.

Whenever the player respawns, it sets to 1(The original speed the player spawns with) instead of the value in the IntValue, so what am i doing wrong?

You’ve to repeat changing the speed of the player if the value changed you can do this by doing the following:

local function PlayerAdded(players)
	players.CharacterAdded:Connect(function(char)
		local hum = char.Humanoid
		local value = players.PlayerGui:WaitForChild("Stats").Buffs.Speed
		hum.WalkSpeed = value.Value
		value.Changed:Connect(function()
			hum.WalkSpeed = value .Value
		end)
		print("speed set")
		hum.JumpPower = 5
		hum.MaxHealth = 10
		char.RightUpperArm.Size = Vector3.new(1, 0.1, 1)
		char.LeftUpperArm.Size = Vector3.new(1, 0.1, 1)
		char.RightLowerArm.Transparency = 1
		char.LeftLowerArm.Transparency = 1
		char.RightHand.Transparency = 1
		char.LeftHand.Transparency = 1
	end)
end

game.Players.PlayerAdded:Connect(PlayerAdded)
1 Like

This still doesn’t seem to fix my problem with the player’s speed setting to 1 whenever they reset and respawn.

Try this instead maybe.

local function PlayerAdded(players)
	local value = players.PlayerGui:WaitForChild("Stats").Buffs.Speed
	players.CharacterAdded:Connect(function(char)
		local hum = char.Humanoid
		hum.WalkSpeed = value.Value
		print("speed set")
		hum.JumpPower = 5
		hum.MaxHealth = 10
		char.RightUpperArm.Size = Vector3.new(1, 0.1, 1)
		char.LeftUpperArm.Size = Vector3.new(1, 0.1, 1)
		char.RightLowerArm.Transparency = 1
		char.LeftLowerArm.Transparency = 1
		char.RightHand.Transparency = 1
		char.LeftHand.Transparency = 1
	end)
	value.Changed:Connect(function()
		if plr.Character and plr.Character:FindFirstChild("Humanoid")
			hum.WalkSpeed = value .Value
		end
	end)
end

Seems like this script itself isn’t working

Did you add the following to the script?

game.Players.PlayerAdded:Connect(PlayerAdded)
1 Like

Yes, the problem is that in your code, the last bit of codes don’t know what is “hum” and “plr”. I really don’t know how to fix my previous scripts.

That’s probably happening because the IntValue is 1?

Are you changing your IntValue through a localscript? If so, the server wouldn’t see the same value as on the client.

If this is the issue you are looking to fix, try experimenting with CharacterAdded instead of PlayerAdded. The difference is that CharacterAdded fires everytime the player character respawns, while PlayerAdded only fires everytime a Player is added (when a player joins).

Hope this helps!

1 Like

He already has a CharacterAdded event on the script though? :thinking:

True, but it seems to be nested under PlayerAdded (which would need to fire first before CharacterAdded would); correct me if I’m wrong.

local players = game:GetService("Players")
    players.CharacterAdded:Connect(function(char)
    	char.Humanoid.WalkSpeed = players.PlayerGui:WaitForChild("Stats").Buffs.Speed.Value
    	print("speed set")
    	char.Humanoid.JumpPower = 5
    	char.Humanoid.MaxHealth = 10
    	char.RightUpperArm.Size = Vector3.new(1, 0.1, 1)
    	char.LeftUpperArm.Size = Vector3.new(1, 0.1, 1)
    	char.RightLowerArm.Transparency = 1
    	char.LeftLowerArm.Transparency = 1
    	char.RightHand.Transparency = 1
    	char.LeftHand.Transparency = 1
    end)

I think this script might work better since CharacterAdded is at the top, you don’t really need PlayerAdded for your use case, to my knowledge. (For future use: this script I posted will not work, please do not use it as is.)

From what I know the CharacterAdded event requires the Player object, not the Player service.

PlayerAdded is used before it because it returns the Player object and then you use it on the CharacterAdded event, which is necessary.

There’s nothing wrong with using PlayerAdded event also since everytime a player joins the game it’ll be activated and CharacterAdded will also be connected to the player.
CharacterAdded will fire everytime the player spawns/respawns It does not need PlayerAdded to fire once again.

Thank you for pointing this out, this was absolutely my mistake. I have one question for OP, is this a LocalScript or a ServerScript?

It’s a Script in Workspace, when the player joins, it sets the player’s speed according to the IntValue which starts at 1, when the IntValue changes and the player resets, logically it should set it to the IntValue, but it somehow gets the value of 1, what’s happening? (I check what value it is in the PlayerGui and it is the correct value, which let’s say is… 7 at this moment.)

Are you changing the IntValue through a localscript or the serverscript?
Can you print the IntValue through the serverscript and see if It’s 1?

As I said if you change it through a localscript the server will not see the same as the client does.

The script that increases the value is a LocalScript, because you increase the value by clicking on a GUI if you have enough points to increase it (IntValue inside GUI too.), is there a way without a local script?

Perhaps confusingly you are using argument called players instead of player or plr, which is more common. PlayerAdded provides single player. This is not an error, but you may want to change it.

Now on to the topic:
There is no way this could work the way you describe, unless your gui is set not to reset on spawn (ResetOnSpawn) on your main screen is deselected. (default is “on”) If it is on, player GUI will be copied from StarterGui and old one lost. So your value will be reset to 1 (or whatever is set in StarterGui)

If you can fix this, bear in mind that this is a very exploit prone method to keep track of player in-game data.

1 Like

The IntValue resets back to 1 in the StarterGui, no matter if ResetOnSpawn is on, however since it doesn’t reset in the PlayerGui, i thought i would utilize it.

Would you consider moving all of the IntValues onto ServerStorage, only with the client sending RemoteEvents to update the value? That way, the GUI is simply a sort of “fire” button for the RemoteEvent.

I will try and update you guys.

1 Like