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

My script wont increase walkspeed?

Asked by 4 years ago

My script isn't working. I'm trying to make a medieval battle game and i have an intvalue inside the player character called "Horses". I want to make it increase the speed if you have more horses. I've tried putting it in startercharacterscripts, replicatedfirst, and serverscriptservice. Here is the code:

01local player = game.Players.LocalPlayer
02local character = player.Character or player.CharacterAdded:Wait()
03local horses = character:WaitForChild("Horses").Value
04 
05 
06horses.Changed:Connect(function()
07    character:FindFirstChild("Humanoid").Walkspeed = 5
08    if horses > 0 then
09        character:FindFirstChild("Humanoid").WalkSpeed = character:FindFirstChild("Humanoid").WalkSpeed + horses * 0.35
10    end
11 
12end)
0
Is it erroring? Also I would put if horses.Value > 0 and just local horses = character:WaitForChild("Horses"). iNot_here 93 — 4y

2 answers

Log in to vote
1
Answered by
tjtorin 172
4 years ago

The problem is that the Changed event has a property parameter which in your case would be the value of horses. For example if the value of horses was changed to 5 then the parameter would say 5 and you should be using that instead of just saying "horses > 0". You also said "Walkspeed" instead of "WalkSpeed" on line 7. Make sure you are also using a local script if you ever use game.Players.LocalPlayer

01local player = game.Players.LocalPlayer
02local character = player.Character or player.CharacterAdded:Wait()
03local horses = character:WaitForChild("Horses").Value
04 
05horses.Changed:Connect(function(property)
06    character:FindFirstChild("Humanoid").WalkSpeed = 5
07    if property > 0 then
08        character:FindFirstChild("Humanoid").WalkSpeed = character:FindFirstChild("Humanoid").WalkSpeed + property * 0.35
09    end
10end)
0
It still wont work, i did what you said and it wont change walkspeed. CxldMoon 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

On line 3, you are taking the value of "Horses", rather than creating a reference to the object. This means that the function at line 5 will never detect a change in the value. Change line 3 to this:

1local horses = character:WaitForChild("Horses")

This will reference the object, allowing it to detect a change.

Answer this question