How to stop a loop with remote event?

First here are my scripts:
Client

local userInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local horseWalkEvnt = ReplicatedStorage.HorseEvents:WaitForChild("HorseWalk")
local turnRightEvnt = ReplicatedStorage.HorseEvents:WaitForChild("TurnRight")

userInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W and mounted == true then	--walk command
		walking = true
		horseWalkEvnt:FireServer(player)
	end
end)

userInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.D and walking == true then	--Turn right 
		print("began")
		turnRightEvnt:FireServer(false)
	end
end)

userInputService.InputEnded:Connect(function(key)
	elseif key.KeyCode == Enum.KeyCode.D and walking == true then	--stop turning
		print("stopped")
		turnRightEvnt:FireServer(true)
	end
end)

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local horseWalkEvnt = ReplicatedStorage:WaitForChild("HorseEvents"):WaitForChild("HorseWalk")
local turnRightEvnt = ReplicatedStorage:WaitForChild("HorseEvents"):WaitForChild("TurnRight")

local function turnRight(player,turning)			-- turn right
	print(turning)
	while turning == false do
		for x = 1,4,4 do
			horse:SetPrimaryPartCFrame(horse:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,math.rad(-x),0))
		end
		wait()
	end
end

local function horseWalk(player)	--Move forward
	PlayWalkAnim:Play()
	while wait()do						
		horse:SetPrimaryPartCFrame(horse:GetPrimaryPartCFrame()*CFrame.new(0,0,0-.2))
	end	
end

horseWalkEvnt.OnServerEvent:Connect(horseWalk)
turnRightEvnt.OnServerEvent:Connect(turnRight)

These scripts are made for controlling a horse. The problem is that the horse would never stop turning after once firing the turnRightEvnt. I have tried many different loops, breaking, and dsiconnecting the function. robloxapp-20200530-2217450.wmv (2.4 MB) Without the lag it would move smoothly. According to the output everything is working fine, but the loop never stops.
Output for one key Code D:
began
false
stopped
true
If I press D again (as I did in the video) the horse starts turning faster. I’m doubting, if this is even the right way to make a horse move…

What you could do is set up variables outside of the function and manipulate it through remove events.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local horseWalkEvnt = ReplicatedStorage:WaitForChild("HorseEvents"):WaitForChild("HorseWalk")
local turnRightEvnt = ReplicatedStorage:WaitForChild("HorseEvents"):WaitForChild("TurnRight")
local turningRight = true

local function turnRight(player, turning)			-- turn right
	turningRight = turning
	while turningRight == false do
		for x = 1,4,4 do
			horse:SetPrimaryPartCFrame(horse:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,math.rad(-x),0))
		end
		wait()
	end
end

local function horseWalk(player)	--Move forward
	PlayWalkAnim:Play()
	while wait()do						
		horse:SetPrimaryPartCFrame(horse:GetPrimaryPartCFrame()*CFrame.new(0,0,0-.2))
	end	
end

horseWalkEvnt.OnServerEvent:Connect(horseWalk)
turnRightEvnt.OnServerEvent:Connect(turnRight)

Unrelated, but the way you handle the checks for the horse’s control is bad UX-wise. Running the data through the server then back to the client creates noticeable latency in responsiveness.

2 Likes

Just a question from this response. If the network owner of the horse were to be set to the player, would local manipulation on the horse replicate to the server?

1 Like

Yes, this is mainly what Network Ownership is used for.

2 Likes