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
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)