Hi, I’ve trying to develop a system where everytime a player clicks a button, a GUI will be cloned for them and is visible for everyone including newcomers. I’ve successfully made the script to replicate it to all clients, but when it does, it clones twice. I’m also aware that this script may not work if a new player joins the server as the event fired once. Can anyone help me please?
-- Local Script
debounce = false
buy.MouseButton1Click:Connect(function()
if debounce == false
then debounce = true
event:FireServer(ducats, townname)
print("1")
end
debounce = false
end)
event2.OnClientEvent:Connect(function(player, townname)
print("ok2")
local newgui = shopgui:Clone()
newgui.StoreName.Text = player.Name .. "'s Store"
newgui.Parent = game.Players.LocalPlayer.PlayerGui.TownGui:WaitForChild(townname).FirstFrame.TradeFrame.ScrollingFrame
end)
-- Server script
debounce = false
event.OnServerEvent:Connect(function(player, money, townname)
print("OK2")
if debounce == false then
debounce = true
wait()
if money.Value >= 1000 then
money.Value = money.Value - 1000
event2:FireAllClients(player, townname)
end
debounce = false
end
end)
In order for the gui to not clone 2 times you would need to add a statement where it checks if the gui has already existed.
event2.OnClientEvent:Connect(function(player, townname)
print("ok2")
if not game.Players.LocalPlayer.PlayerGui.TownGui:WaitForChild(townname).FirstFrame.TradeFrame.ScrollingFrame:FindFirstChild("GUI NAME HERE") then else return end
local newgui = shopgui:Clone()
newgui.StoreName.Text = player.Name .. "'s Store"
newgui.Parent = game.Players.LocalPlayer.PlayerGui.TownGui:WaitForChild(townname).FirstFrame.TradeFrame.ScrollingFrame
end)
You could just simplify and correct things by not calling WaitForChild on the Gui as if it didn’t exist soon enough it would throw. The else block is not needed if you use a non-invert if statement.
@Askdet If you need to check whether a Gui currently exists, use FindFirstChild to verify existence
local PGui = player:WaitForChild("PlayerGui") -- consumes no budget if instance exists already
if not PGui:FindFirstChild("SomeScreenGui") then
-- clone it
end
There’s also no cooldown for your debounce script, so that won’t work.
Ah well, thanks it works but I have another issue in regarding the GUI not visible for newcomers, is there a way around this? It’s only visible for those in the server. Does it mean I have to clone the GUI again into StarterGui?
If you put a Gui in game.StarterGui it’ll be replicated into every player’s PlayerGui, otherwise you could use PlayerAdded and clone a Gui manually into a player’s PlayerGui.
Edit:
Just clone the Gui into any player’s PlayerGui each time they click the button then.