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:
01 | local player = game.Players.LocalPlayer |
02 | local character = player.Character or player.CharacterAdded:Wait() |
03 | local horses = character:WaitForChild( "Horses" ).Value |
04 |
05 |
06 | horses.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 |
12 | end ) |
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
01 | local player = game.Players.LocalPlayer |
02 | local character = player.Character or player.CharacterAdded:Wait() |
03 | local horses = character:WaitForChild( "Horses" ).Value |
04 |
05 | horses.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 |
10 | end ) |
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:
1 | local horses = character:WaitForChild( "Horses" ) |
This will reference the object, allowing it to detect a change.