Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Increasing a players walkspeed by purchasing with coins not working correctly?

Asked by 3 years ago

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

2 answers

Log in to vote
0
Answered by 3 years ago

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)
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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:

  1. Listen on the server event "SPHandler" game.ReplicatedStorage.Events.SPHandler.OnServerEvent:Connect(function(player)

  2. Check value of Coins. If it is over or equal 100, progress. if player.leaderstats.Coins.Value >= 100 then

  3. 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

  4. Remove 100 coins from the player player.leaderstats.Coins.Value = player.leaderstats.Coins.Value -100

  5. Add 1 to the local variable "walkspeed" walkspeed = walkspeed +1

  6. (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)

Answer this question