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

How to make a RemoteEvent fire only once?

I am working on a Obby and I want to send a StarterGui SetCore SendNotificaiton but once the player reaches the end it spams every player infinitely with it? How do I make it only send once?

Here is my Code:

Client-Notification (in StarterGui):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local Notification = ReplicatedStorage:WaitForChild("Notification")

Notification.OnClientEvent:connect(function(Title, Description, Image)
	StarterGui:SetCore("SendNotification", {
			Title = Title,
			Text = Description,
			Duration = 3,
			Button1 = "Ok"
		})
	
end)

Server-Notification (In ServerScriptService):

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Notification = ReplicatedStorage:WaitForChild("Notification")

while true do
	wait(0.1)
	for i, v in pairs(game.Players:GetPlayers()) do
		
		if v.leaderstats.Level.Value == 7 then
			Notification:FireAllClients(v.Name.." Finished", v.Name.." has finished the Obby!")
		end
		
	end
	
end

2 comments

0

You should clone the remote, and then after they finish it you should fire the clone and then destory the clone.

1

Or you can use the :Disconnect function. https://developer.roblox.com/en-us/articles/events/index.html

1 answer

In your case, your loop will identify which Player has essentially beaten the Obby, and continuously fire to everyone because that condition will always remain true, until their Level becomes less than your pass limit. It’s best if you Connect a GetPropertyChangedSignal event to each players Level instead of using an infinite loop, you can do that this way:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Notification = ReplicatedStorage:WaitForChild("Notification")

local Players = game:GetService("Players")

local CheckIfPlayerWon(Player)
   local Leaderstats = Player:WaitForChild("leaderstats")
   local Level = Leaderstats:WaitForChild("Level")
   Level:GetPropertyChangedSignal("Value"):Connect(function(newValue)
       if (newValue >= 7) then
           Notification:FireAllClients()
       end
   end)
end)

Players.PlayerAdded:Connect(function(CheckIfPlayerWon)

This should only fire to everyone once, and if their level surpasses 7 at any time. If you wish to have an Event only run a single time, you can use the Disconnect method to unlink the listener.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Notification = ReplicatedStorage:WaitForChild("Notification")
local Connection

local Players = game:GetService("Players")

local CheckIfPlayerWon(Player)
   local Leaderstats = Player:WaitForChild("leaderstats")
   local Level = Leaderstats:WaitForChild("Level")
   Connection = Level:GetPropertyChangedSignal("Value"):Connect(function(newValue)
       if (newValue >= 7) then
           Notification:FireAllClients()
           Connection:Disconnect()
       end
   end)
end)

Players.PlayerAdded:Connect(function(CheckIfPlayerWon)

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