Problem while learning RemoteEvents

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 

Thanks for helping! :slightly_smiling_face:

1 Like

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

Ah, whoops. How would I implement this into my script?

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

I understand this, thanks for helping. It’ll make me learn scripting well!

1 Like

@FrankieRichBoy also try my edited version with RemoteFunction.

I surely will! It looks very good, thanks for helping me out!

1 Like

No problem glad that we helped you!
Please tell me if you found any issue with the code I wrote it on mobile :D.

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.

Oh yes make sense I will edit it a bit thanks for pointing that out.

I try this to see if it works.

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)

Learn more about remote event from here.