I am trying to make a rideable horse. I decided to do this by making it so that everytime you press w, you go up one gait. This is how I am writing the code, however, the horse does not move at all.
Here is the server script.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WFunction = ReplicatedStorage:WaitForChild("WFunction")
local SFunction = ReplicatedStorage:WaitForChild("SFunction")
local HorseTest = workspace.Horse
local SitPart = HorseTest.HorseTest
local RidingValue = SitPart.RidingValue
local MovementValue = SitPart.MovementValue
local defstats = {
-10,
0,
10,
20,
30,
40
}
local gaits = {
"Back",
"Idle",
"Walk",
"Trot",
"Canter",
"Gallop"
}
WFunction.OnServerInvoke = function(player)
local BodyVelocity = SitPart:FindFirstChild("BodyVelocity")
local character = player.character
BodyVelocity.Velocity = Vector3.new(defstats[2],0,0)
if BodyVelocity.Velocity == defstats[1] then
print("2")
BodyVelocity.Velocity = Vector3.new(defstats[2],0,0)
MovementValue.Value = gaits[2]
end
if BodyVelocity.Velocity == defstats[2] then
print("3")
BodyVelocity.Velocity = Vector3.new(defstats[3],0,0)
MovementValue.Value = gaits[3]
end
if BodyVelocity.Velocity == defstats[3] then
print("4")
BodyVelocity.Velocity = Vector3.new(defstats[4],0,0)
MovementValue.Value = gaits[4]
end
if BodyVelocity.Velocity == defstats[4] then
print("5")
BodyVelocity.Velocity = Vector3.new(defstats[5],0,0)
MovementValue.Value = gaits[5]
end
if BodyVelocity.Velocity == defstats[5] then
print("6")
BodyVelocity.Velocity = Vector3.new(defstats[6],0,0)
MovementValue.Value = gaits[6]
end
end
SFunction.OnServerInvoke = function(player)
local BodyVelocity = SitPart:FindFirstChild("BodyVelocity")
local character = player.character
if BodyVelocity.Velocity == defstats[6] then
print("-5")
BodyVelocity.Velocity = Vector3.new(defstats[5],0,0)
MovementValue.Value = gaits[5]
end
if BodyVelocity.Velocity == defstats[5] then
print("-4")
BodyVelocity.Velocity = Vector3.new(defstats[4],0,0)
MovementValue.Value = gaits[4]
end
if BodyVelocity.Velocity == defstats[4] then
print("-3")
BodyVelocity.Velocity = Vector3.new(defstats[3],0,0)
MovementValue.Value = gaits[3]
end
if BodyVelocity.Velocity == defstats[3] then
print("-2")
BodyVelocity.Velocity = Vector3.new(defstats[2],0,0)
MovementValue.Value = gaits[2]
end
if BodyVelocity.Velocity == defstats[2] then
print("-1")
BodyVelocity.Velocity = Vector3.new(defstats[1],0,0)
MovementValue.Value = gaits[1]
end
end
And here is the local script.
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HorseTest = workspace.Horse
local SitPart = HorseTest.HorseTest
local RidingValue = SitPart.RidingValue
local MovementValue = SitPart.MovementValue
MovementValue.Value = "Idle"
local WFunction = ReplicatedStorage:WaitForChild("WFunction")
local SFunction = ReplicatedStorage:WaitForChild("SFunction")
local gaits = {
"Back",
"Idle",
"Walk",
"Trot",
"Canter",
"Gallop"
}
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then
if RidingValue.Value == true then
WFunction:InvokeServer()
if MovementValue.Value == gaits[1] then
print("1")
MovementValue.Value = gaits[2]
end
if MovementValue.Value == gaits[2] then
print("2")
MovementValue.Value = gaits[3]
end
if MovementValue.Value == gaits[3] then
print("3")
MovementValue.Value = gaits[4]
end
if MovementValue.Value == gaits[4] then
print("4")
MovementValue.Value = gaits[5]
end
if MovementValue.Value == gaits[5] then
print("5")
MovementValue.Value = gaits[6]
end
end
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down then
if RidingValue.Value == true then
SFunction:InvokeServer()
if MovementValue.Value == gaits[6] then
print("-6")
MovementValue.Value = gaits[5]
end
if MovementValue.Value == gaits[5] then
print("-5")
MovementValue.Value = gaits[4]
end
if MovementValue.Value == gaits[4] then
print("-4")
MovementValue.Value = gaits[3]
end
if MovementValue.Value == gaits[3] then
print("-3")
MovementValue.Value = gaits[2]
end
if MovementValue.Value == gaits[2] then
print("-2")
MovementValue.Value = gaits[1]
end
end
end
end)
If you know of a more efficient and easier way to write this code, please let me know, and I will fix it.
Currently, when I press W, it prints all the messages from all if statements from the local script, and the same with pressing S, and the horse doesn’t move at all. I check the BodyVelocity, and the value that I am trying to change is at 0, and the MovementValue Value is “Idle”