Horse not moving

Basically I was trying to create a really simple movement script for my horse model. However the horse isn’t moving and there aren’t any errors in the output. What did I do wrong?

This is the source code of the local script I have inside the horse model:

local horse = script.Parent.HRP
local uis = game:GetService("UserInputService")
local bv = Instance.new("BodyVelocity", horse)
local accelerate = false 
local speed = 0 
bv.Velocity = Vector3.new() 
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
local bg = Instance.new("BodyGyro",horse) 
bg.CFrame = CFrame.new() 
bg.MaxTorque = Vector3.new(math.huge,math.huge,math.huge) 

uis.InputBegan:Connect(function(input,istyping)
	if horse.Seat.Occupant ~= game.Players.LocalPlayer.Character.Humanoid then return end 
	if not istyping then
		if input.KeyCode == Enum.KeyCode.W then 
			accelerate = true
			repeat
				speed = speed + 1
				bv.Velocity = horse.CFrame.LookVector * speed 
				bg.CFrame = horse.CFrame
				wait(.5)
			until accelerate == false
		end
		if input.KeyCode == Enum.KeyCode.S  then
			accelerate = true
			if speed > -300 then
				repeat
					speed = speed - 1
					bv.Velocity = horse.CFrame.LookVector * speed 
					bg.CFrame = horse.CFrame
					print(speed)
					print(speed < -300)
					wait(.5)
				until accelerate == false or speed < -300 
			else
				speed = 0
			end
		end
		if input.KeyCode == Enum.KeyCode.A then 
			bg.CFrame = horse.CFrame * CFrame.Angles(0,math.rad(150),0)
		end
		if input.KeyCode == Enum.KeyCode.D then
			bg.CFrame = horse.CFrame * CFrame.Angles(0,math.rad(-150),0)
		end
	end
end)

uis.InputEnded:Connect(function(input,istyping)
	if horse.Seat.Occupant ~= game.Players.LocalPlayer.Character.Humanoid then return end
	if not istyping then
		if input.KeyCode == Enum.KeyCode.W then
			bg.CFrame = horse.CFrame
			accelerate = false
			repeat wait() 
				bv.Velocity = horse.CFrame.LookVector * speed  
				speed = speed - 1 
			until 
			speed == 0 or speed < 0 or accelerate == true
			if speed <= 0 and accelerate == false then
				speed = 0
				bv.Velocity = Vector3.new()
			end
		end
		if input.KeyCode == Enum.KeyCode.S  then
			bg.CFrame = horse.CFrame
			accelerate = false
			if speed < 0 then
				repeat wait() 
					bv.Velocity = horse.CFrame.LookVector * speed
					speed = speed + 1 
				until speed > 0 or accelerate == true
				if speed > 0 and accelerate == false then 
					speed = 0
					bv.Velocity = Vector3.new()
				end
			end
		end
	end
end)

I don’t see any istyping variable declared, could that be the problem?

@Soflowsen Its the second paramter in the Event.

The horse isn’t anchored or anything right? If you check in the properties tab, does the BodyVelocity change?

None of the parts are anchored (I can even push the horse over), and the BodyVelocity of the HRP in the properties tab does not change

Maybe try adding prints to see where it breaks?

I added a print right after

if input.KeyCode == Enum.KeyCode.W then

and nothing happened, so I’m guessing the problem is before that.

Have you made the rider the network owner of the horse?
If the physics are handled by the server or another player then the horse won’t move based on forces from the client side.

I don’t think you can put local scripts inside server instances. userInputService only works when you put the local script inside the player.

Edit: Though, even after you put it inside the player, you need to have another script in the horse, and use remoteEvents to fire an event from the player local script, to the horse script, to move. Or else, the local script won’t replicate to the server and other clients (The horse will only move on the client/player that runs the local script, not other clients/players, unless you use remoteEvents)

The local script won’t run unless in the player’s backpack, the character, or playergui

I would check this Video out to help you, you can modify it to your liking but it is very helpful.

Alright I will check it out. I appreciate all the help. :sweat_smile:

1 Like

You may want to handle movement locally, and have the server set the player as the network owner of the horse. That way you will not have to deal with the delay that comes from remote events, and the movement will be replicated to the server for everyone to see.

1 Like

That would be a good idea, but if you do that you should also make an anti-exploit that checks the horse’s position every 2 seconds, to make sure the horse isn’t moving too fast, because exploiters can easily change the position of the baseparts they have the network ownership of.