Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Any way to stop a Remote Function within a loop? (Code included)

Hey guys,

So my latest game uses Remote Functions to launch a GUI on the player's screen. This function is then located within a while true loop.

The rest of the script functions as it should, but when it loops around the remote event does not work since it is still broadcasting from the last time. Here is an idea of the code used below.

--Server Script
local raceLight = Instance.new("RemoteFunction")
raceLight.Name = "raceLights"
raceLight.Parent = game.Workspace

while true do
--Stuff about loading the map here
wait(1)
function raceLight.OnServerInvoke()
	print("sending out function")
	return "success"
end
--Stuff about the race and removing the map here
end

^^^This is not the full code, just a snippet where I have the problem!^^^

--Local Script located in StarterPlayerScripts
local message = game.Workspace.raceLight:InvokeServer()
	game.Players.LocalPlayer.PlayerGui.StartingLights.Lights.Visible = true
	wait(2)
	game.Players.LocalPlayer.PlayerGui.StartingLights.Lights.Seq.Disabled = false
print(message)

Is there a way to get the function to stop broadcasting, or should I try another Remote Event style?

2 comments

0

Why are you setting a new callback to OnServerInvoke every second? This should only be done once, and outside of any loop. You should also take a look at code indentation: http://mrbool.com/importance-of-code-indentation/29079

0

The code has about 50 other lines. I just removed it to the line that matters (abstraction for the Computer Science OCR student... ????) In reality, it's every 60 seconds the loop comes back around to it.

1 answer

Place the OnServerInvoke function outside the loop (after creating RemoteFunction object and before line with while true do )

This discussion is preserved as it was. New answers, comments, and votes are closed.