The code below in supposed to subtract 100 coins from the player (This works) and add +1 walkspeed everytime it is purchased but for some reason it in not working.
code:
game.ReplicatedStorage.Events.SPHandler.OnServerEvent:Connect(function(player) if player.leaderstats.Coins.Value >= 100 then local walkspeed = player.Character.Humanoid.WalkSpeed player.leaderstats.Coins.Value = player.leaderstats.Coins.Value -100 walkspeed = walkspeed +1 end end)
errors: no errors
My answer is that you should remove the .WalkSpeed from line 3 and add .WalkSpeed on the walkspeed locals in line 5. To understand me easier, you should replace your current code with this:
game.ReplicatedStorage.Events.SPHandler.OnServerEvent:Connect(function(player) if player.leaderstats.Coins.Value >= 100 then local humanoid = player.Character.Humanoid player.leaderstats.Coins.Value = player.leaderstats.Coins.Value -100 humanoid.WalkSpeed = humanoid.WalkSpeed + 1 end end)
You are setting the value of a local variable you created, the walkspeed of the player is not affected.
Step by step, this is what your current code does:
Listen on the server event "SPHandler"
game.ReplicatedStorage.Events.SPHandler.OnServerEvent:Connect(function(player)
Check value of Coins. If it is over or equal 100, progress.
if player.leaderstats.Coins.Value >= 100 then
Assign the value of Humanoid.WalkSpeed to a local variable.
This is crucial, because you are not making a reference to the property, you are getting the value of the property and making a new local variable, containing the value.
local walkspeed = player.Character.Humanoid.WalkSpeed
Remove 100 coins from the player
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value -100
Add 1 to the local variable "walkspeed"
walkspeed = walkspeed +1
(a bunch of ends)
To fix this issue, you should, instead of doing this:
game.ReplicatedStorage.Events.SPHandler.OnServerEvent:Connect(function(player) if player.leaderstats.Coins.Value >= 100 then local walkspeed = player.Character.Humanoid.WalkSpeed player.leaderstats.Coins.Value = player.leaderstats.Coins.Value -100 **walkspeed = walkspeed +1** end end)
Change the line in bold to
player.Character.Humanoid.WalkSpeed = walkspeed + 1
Or:
game.ReplicatedStorage.Events.SPHandler.OnServerEvent:Connect(function(player) if player.leaderstats.Coins.Value >= 100 then local walkspeed = player.Character.Humanoid.WalkSpeed player.leaderstats.Coins.Value = player.leaderstats.Coins.Value -100 player.Character.Humanoid.WalkSpeed = walkspeed +1 end end)