Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a RemoteEvent fire only once?

Asked by 4 years ago
Edited 4 years ago

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
0
You should clone the remote, and then after they finish it you should fire the clone and then destory the clone. User#32819 0 — 4y
1
Or you can use the :Disconnect function. https://developer.roblox.com/en-us/articles/events/index.html killerbrenden 1537 — 4y

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)

Ad

Answer this question