So, I am basically as learning process learning how RemoteEvents work. I have put up a simple repeat block, that keeps repeating making parts until remote event is fired. But, how do I make it stop as soon as the remote event is fired?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
local partcounter = 0
repeat
wait(0.1)
local part = Instance.new("Part")
partcounter = partcounter + 1
until
You can use RemoteFunction to return information to stop.
--local script
local MessageToStop
MessageToStop = RemoteF:InvokeServer()
local counter = 0
repeat
counter + 1
wait(0.1)
until MessageToStop == "success"
--server script
local RemoteF = game:GetService ("ReplicatedStorage").RemoteFunction
local function GetMessage(player)
return "success"
end
RemoteF.OnServerInvoke = GetMessage
It depends on how your system is set up. If the loop is in the same palce as where you set up the Event, you can do a system like this
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
local partcounter = 0
local stop = false
remoteEvent.OnServerEvent:Connect(function()
stop = true
end)
repeat
wait(0.1)
local part = Instance.new("Part")
partcounter += 1
until stop
print(partcounter)
It’ll stop making parts as soon as the Remote was Fired by a client
This would repeat forever since you only invoke the server once and not inside the actual repeating loop itself therefore this would probably end up crashing the game if you’re just making new parts.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
local partcounter = 0
repeat
wait(0.1)
local part = Instance.new("Part")
partcounter = partcounter + 1
RemoteEventTest.OnServerEvent:Connect(RemoteEventTestFired)