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

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local horses = character:WaitForChild("Horses").Value


horses.Changed:Connect(function()
    character:FindFirstChild("Humanoid").Walkspeed = 5
    if horses > 0 then
        character:FindFirstChild("Humanoid").WalkSpeed = character:FindFirstChild("Humanoid").WalkSpeed + horses * 0.35
    end

end)
0
Is it erroring? Also I would put if horses.Value > 0 and just local horses = character:WaitForChild("Horses"). iNot_here 93 — 3y

2 answers

Log in to vote
1
Answered by
tjtorin 172
3 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

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local horses = character:WaitForChild("Horses").Value

horses.Changed:Connect(function(property)
    character:FindFirstChild("Humanoid").WalkSpeed = 5
    if property > 0 then
        character:FindFirstChild("Humanoid").WalkSpeed = character:FindFirstChild("Humanoid").WalkSpeed + property * 0.35
    end
end)
0
It still wont work, i did what you said and it wont change walkspeed. CxldMoon 0 — 3y
Ad
Log in to vote
0
Answered by 3 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:

local horses = character:WaitForChild("Horses")

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

Answer this question