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

How to fix walkspeed script?

Asked by 9 years ago

Hello, I have problem with my walkspeed script when player join to game. So in my place i have something like a "speed shop", when player join to game script set his walkspeed on 25, but if player buy +30 walkspeed in speed shop it works only for a few seconds and set back 25 walkspeed... Here is walkspeed script:

01function edit(h)
02    if h ~= nil then
03        h.WalkSpeed = 25
04    end
05end
06 
07while true do
08    cur = game.Players:GetChildren()
09    for i = 1, #cur do
10        edit(cur[i].Character.Humanoid)
11        wait(0.1)
12    end
13    wait(4)
14end

Here is script from shop, maybe someone will need it too:

01local dialog = script.Parent
02 
03function answer(player, pick)
04    local char = player.Character
05    local humanoid = char["Humanoid"]
06    local stats = player:findFirstChild("leaderstats")
07    local g = stats:findFirstChild("Points")
08    if pick.Name == "30" then
09        if g.Value >= 500 then
10            humanoid.WalkSpeed = humanoid.WalkSpeed + 30
11            g.Value = g.Value - 500
12 
13        end
14    end
15end
16 
17 
18dialog.DialogChoiceSelected:connect(answer)

It's dialog shop.

2 answers

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

The problem is your WalkSpeed script every few seconds sets everyone's WalkSpeed to 25 - regardless of what speed they bought.

The easiest way to get around this is to only set the WalkSpeed to 25 when the Player's Character spawns:

01function edit(h)
02    if h ~= nil then
03        h.WalkSpeed = 25
04    end
05end
06 
07game.Players.PlayerAdded:connect(function(player)
08    player.CharacterAdded:connect(function(char)
09        edit(char:WaitForChild("Humanoid"))
10    end
11end)
0
Thanks for help. Works perfectly. DevKarolus1 70 — 9y
Ad
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
9 years ago

Problem

The only problem is in your first script. You have the game constantly changing the WalkSpeed. To fix this, you need to only have the WalkSpeed be changed when the Player joins the game.


Fix Fo' Da Script

So now, here's a fix with my method of doing it:

1game.Players.PlayerAdded:connect(function(Plr)
2    Plr.CharacterAdded:connect(function(Char)
3    Speed=25
4    Char:FindFirstChild("Humanoid").WalkSpeed=25
5    end)
6end)

Now this would fix it so that it doesn't constantly mess up the WalkSpeed, but it wouldn't work when your player died. So to fix this, we'll add in this:

01game.Players.PlayerAdded:connect(function(Plr)
02    Plr.CharacterAdded:connect(function(Char)
03        if not Plr:FindFirstChild("Speed") then
04            local Speed=Instance.new("NumberValue",Plr)
05            Speed.Name="PlrSpeed"
06            Speed.Value=25
07        else
08            local Speed=Plr:FindFirstChild("PlrSpeed")
09        end
10        Char:FindFirstChild("Humanoid").WalkSpeed=Speed.Value
11        Char:FindFirstChild("Humanoid").Changed:connect(function(Property)
12            if Property="WalkSpeed" then
13                Speed.Value=Char:FindFirstChild("Humanoid").WalkSpeed
14            end
15        end)
16    end)
17end)

Now this would work to keep the speed updated, even upon death.


Anyways, I hope this helped. If you have any further questions/problems, please leave a comment below, and I'll see what I can do :P

-Dyler3

Answer this question