Script not recognizing IntValue

Hello!

I am trying to set the humanoids walkspeed to an IntValue in a leaderstats script.
image

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	end

	local timePlayed = leaderstats:FindFirstChild("TimePlayed")
	if not timePlayed then
		timePlayed = Instance.new("IntValue") -- different Int value, not related
		timePlayed.Name = "Speed"
		timePlayed.Value = 0
		timePlayed.Parent = leaderstats
	end

	task.spawn(function()
		while true do
			task.wait(1)
			timePlayed.Value = timePlayed.Value + 1 -- Using this for something else
			script.Value.Value = timePlayed.Value --This is the value I'm using
		end
	end)

Every second the first value increases by 1 as intended. I need the 2nd value to copy the first value’s value. I want the second value to be the characters walkspeed.

I have a script in serverscriptservice to set the player speed to value2

function addSpeed(hum)
	spawn(function()
		while wait(1) do	
			hum.WalkSpeed = game.Workspace.leaderstats.Value2.Value/4 -- HERE
            print("The value is:", game.Workspace.leaderstats.Value2.Value) -- Prints 0
 

end
	end)
end



--not part of issue
game.Players.PlayerAdded:Connect(function(player) 
	

	
	local walkspeed
	
	player.CharacterAdded:Connect(function(char)
		

		
		local humanoid = char:WaitForChild("Humanoid")
		
		if walkspeed then
			wait(0.5)
			humanoid.WalkSpeed = walkspeed
		end
		
		addSpeed(humanoid)
		
		humanoid.Died:Connect(function()
			walkspeed = humanoid.WalkSpeed	
			wait(1)

		

			
		end)
		
	end)


end)

Additionally, I have a local script in StarterGui to update the second value to the first.

task.spawn(function()
	while true do
		game.Workspace.leaderstats.Value2.Value = game.Workspace.leaderstats.Value.Value
		wait(0.1)
		print("script running")
		
	end
end)

(I need to have two values for something later)

When I run the game, both values increase as intended, but for some the reason the script thinks that the second value is 0. (Top of 2nd script)

If I change

hum.WalkSpeed = game.Workspace.leaderstats.Value2.Value/4 -- HERE

to the first value, it works fine. But I need it to be value 2. I don’t know why it works for Value 1 and not Value 2.

I’ve been stuck on this for many hours now.
All help is appreciated!

hum.WalkSpeed = math.round(game.Workspace.leaderstats.Value2.Value/4)

I don’t know if rounding the number will work but try it.

1 Like

Sadly not, thanks anyway though.

Are you changing the values locally?

No, I only use a local script to change the second value to value1

You said u were changing the second value to the first using a local script?

server cant see what has been modified by the client.

2 Likes

Yeah I see what you mean, I put it in a script and it works. Will everyone’s speed in the game be changed though? Or just the local player.

This will make every single player’s speed the same value. If you want each player to have their own individual speed, you will need to store Value and Value2 for each player within the Players service (this is done the same way as creating new entries for leaderstats, without parenting it to the folder, and instead, directly to the player).

After that, you would want to run a RemoteEvent from your local script connecting to your main leaderstats script (or even a separate script).

local event = game.ReplicatedStorage:FindFirstChild("ChangeSpeedEvent")

event:FireServer()
local event = game.ReplicatedStorage:FindFirstChild("RemoteEvent")

event.OnServerEvent:Connect(function(plr) -- The first argument of .OnServerEvent will always be the player
	
	local value1 = game.Players:FindFirstChild(plr):FindFirstChild("Value")
	local value2 = game.Players:FindFirstChild(plr):FindFirstChild("Value2")
	
	value2.Value = value1.Value
	
end)

You can learn more about Remote Events here: