Only activate with a remote

I would like for this to only fire whenever I fire all clients. I tried this, and it didn’t work for some reason… Here’s my current code:

Client:

local remote = game:GetService("ReplicatedStorage").Main

remote.OnClientEvent:Connect(function()
	local rs = game:GetService("RunService")
	local player = game.Players.LocalPlayer
	local part = game.Workspace.Start
	local random = Random.new(game.ReplicatedStorage.Seed.Value)

	local stages = {}
	local currentStageCount = 0
	local maxStageCount = 15

	local children = game.ReplicatedStorage.Stages:GetChildren()
	table.sort(children, function(a, b) return a.Name < b.Name end)

	for i, child in ipairs(children) do
		local chance = child:GetAttribute("Chance") or 1
		for i = 1, chance do
			table.insert(stages, child)
		end
	end

	rs.Heartbeat:Connect(function(deltaTime)
		if player.Character == nil then
			return
		end

		if (part.Position - player.Character:GetPivot().Position).Magnitude > 200 then
			return
		end

		if currentStageCount < maxStageCount then
			local stage = stages[random:NextInteger(1, #stages)]:Clone()
			stage:PivotTo(part.CFrame)
			stage.Start:Destroy()
			stage.End.Transparency = 1
			stage.Parent = workspace.Map

			part:Destroy()
			part = stage.End
			currentStageCount = currentStageCount + 1
		elseif currentStageCount == maxStageCount then
			local endStage = game.ReplicatedStorage.EndStage:Clone()
			endStage:PivotTo(part.CFrame)
			endStage.Start:Destroy()
			endStage.End.Transparency = 1
			endStage.Parent = workspace.Map

			part:Destroy()
			part = endStage.End
			currentStageCount = currentStageCount + 1
		end
	end)

end)

Server:

local timer = 10
local rem = game.ReplicatedStorage.Main

local function formatTime(seconds)
	local min = math.floor(seconds / 60)
	local sec = seconds % 60
	return string.format("%i:%02i", min, sec)
end

while true do

	repeat
		task.wait(1 / game.ReplicatedStorage.PlayersCompleted.Value)
		timer -= 1
		print("Formatted: ".. formatTime(timer))
		print("Actual: ".. timer)
	until timer == 0
	
	timer = 10
    rem:FireAllClients()
end

Any suggestions?

Does it not run at all?

Sidenote: Using a :Connect() in a :Connect() will result in stacking connections. You will need to disconnect the connection when appropriate.

Alternatively, you could have a variable being set to true inside the .OnClientEvent:Connect() and the heartbeat connection return unless the variable is true.

So what its supposed to do is generate a random course from a folder in ReplicatedStorage, but it does nothing at all unfortunately, however ill try your potential fixes and see what happens

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.