The bug that occurs in my script is that whenever the remove event that is connected to my ui opening is fired it duplicates the ui making it so whenever i buy something afterwards it duplicates when It’s only supposed to be purchased once, last night I didn’t know the problem and I ended up figuring out what was the problem now I just need help knowing how to solve it.
– LOCAL SCRIPT
ReplicatedStorage.Remotes.UIRemotes.UI_BoardStart.OnClientEvent:Connect(function(TeamName)
local Tycoon = Workspace:WaitForChild("Tycoon").Tycoons:WaitForChild(TeamName)
local Board = Tycoon.PurchasedObjects.Office.Whiteboard
local BoardCam = Board.BoardCamera
local PlayerName = LocalPlayer.Name
local Owner = Tycoon.TycoonInfo.Owner
if Owner.Value.Name == PlayerName then
Camera.CameraType = Enum.CameraType.Scriptable
--// Some Presets
MainFrame.TeamName.Text = LocalPlayer.ValuesGame.TeamName.Value
--// Labels
MainFrame.Exit.MouseButton1Click:Connect(function(plr)
ClickUI:Play()
MainFrame.Visible = false
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = LocalPlayer.Character.Humanoid
end)
MainFrame.PassButton.MouseButton1Click:Connect(function(plr) -- PASS PURCHASE ATTRIBUTES SCREEN
ClickUI:Play()
MainFrame.PassPurchase.Visible = true
end)
MainFrame.PassPurchase.Purchase.MouseButton1Click:Connect(function(plr)
if LocalPlayer.ValuesGame.Team.PassGame.Value >= 10 then
print("Pass Game Maxed Out")
MainFrame.PassPurchase.PurchaseLabel.Text = "Your pass attributes are maxed out!"
task.wait(3)
MainFrame.PassPurchase.PurchaseLabel.Text = "Purchase 1 Pass Attribute For ".. CurrentPassCost .. " Cash?"
else
if LocalPlayer.leaderstats.Cash.Value <= CurrentPassCost then
ErrorUI:Play()
MainFrame.PassPurchase.PurchaseLabel.Text = "You cannot afford an attribute!"
task.wait(3)
MainFrame.PassPurchase.PurchaseLabel.Text = "Purchase 1 Pass Attribute For ".. CurrentPassCost .. " Cash?"
else
ReplicatedStorage.Remotes.Tycoon_Function_Remotes.White_Board.PurchasePassAttribute:FireServer(CurrentPassCost,TeamName)
print("" ..LocalPlayer.Name.. " Purchased an attribute for ".. CurrentPassCost .. "")
MainFrame.PassPurchase.Visible = false
SoundService.PurchaseCompleted:Play()
-- MainFrame.PassPurchase.Visible = false
end
end
end)
MainFrame.PassPurchase.Decline.MouseButton1Click:Connect(function(plr)
ClickUI:Play()
MainFrame.PassPurchase.Visible = false
end)
--===
MainFrame.RunButton.MouseButton1Click:Connect(function(plr) -- PASS PURCHASE ATTRIBUTES SCREEN
ClickUI:Play()
MainFrame.RunPurchase.Visible = true
end)
MainFrame.RunPurchase.Purchase.MouseButton1Click:Connect(function(plr)
if LocalPlayer.ValuesGame.Team.RunGame.Value >= 10 then
print("Pass Game Maxed Out")
MainFrame.RunPurchase.PurchaseLabel.Text = "Your run attributes are maxed out!"
task.wait(3)
MainFrame.RunPurchase.PurchaseLabel.Text = "Purchase 1 Run Attribute For ".. CurrentPassCost .. " Cash?"
else
if LocalPlayer.leaderstats.Cash.Value <= CurrentPassCost then
ErrorUI:Play()
MainFrame.RunPurchase.PurchaseLabel.Text = "You cannot afford an attribute!"
task.wait(3)
MainFrame.RunPurchase.PurchaseLabel.Text = "Purchase 1 Run Attribute For ".. CurrentPassCost .. " Cash?"
else
ReplicatedStorage.Remotes.Tycoon_Function_Remotes.White_Board.PurchaseRunAttribute:FireServer(CurrentPassCost,TeamName)
print("" ..LocalPlayer.Name.. " Purchased an attribute for ".. CurrentPassCost .. "")
MainFrame.RunPurchase.Visible = false
SoundService.PurchaseCompleted:Play()
-- MainFrame.PassPurchase.Visible = false
end
end
end)
MainFrame.RunPurchase.Decline.MouseButton1Click:Connect(function(plr)
ClickUI:Play()
MainFrame.RunPurchase.Visible = false
end)
--//
local CamTweenBoard = TweenService:Create(Camera,TweenInfo.new(
1,
Enum.EasingStyle.Quad,
Enum.EasingDirection.In),
{
CFrame = BoardCam.CFrame
})
SoundService.Chalk:Play()
CamTweenBoard:Play()
CamTweenBoard.Completed:Wait()
MainFrame.Visible = true
end
end)
This occurs because you’re creating event connections within event connections; everytime UI_BoardStart fires, it will connect and establish new events that’ll fire (as well old ones):
Event:Connect(function(...) -- Every time this event fires
SecondaryEvent:Conect(function(...) -- A new connection here will be established (it does not disconnect old connections)
print(...) -- this will print for every new and pre-existing connection that's been created
end)
end)
In this situation, you’re supposed to store the RBXScriptConnection in a variable and disconnect it when the main event fires:
local oldEvent = nil
Event:Connect(function()
if oldEvent then
oldEvent:Disconnect() -- Disconnect the old event so it doesn't fire
end
oldEvent = SecondaryEvent:Connect(function(...) -- Only the most recent connection will fire
print(...)
end)
end)
Okay, just so I can understand and be able to use this, will the SecondaryEvent be the same event? so would it look something like this?
local oldEvent = nil
ReplicatedStorage.Remotes.UIRemotes.UI_BoardStart.OnClientEvent:Connect(function(TeamName)
if oldEvent then
oldEvent:Disconnect() -- Disconnect the old event so it doesn't fire
end
oldEvent = ReplicatedStorage.Remotes.UIRemotes.UI_BoardStart.OnClientEvent:Connect(function( ...) -- Only the most recent connection will fire
print(...)
--- would this is where I would put my code?
end)
end)
Right, I’m just trying to find the best way to set this up on how your explaining it.
ReplicatedStorage.Remotes.UIRemotes.UI_BoardStart.OnClientEvent:Connect(function(TeamName, ...) -- Every time this event fires
MainFrame.Exit.MouseButton1Click:Conect(function(plr, ...) -- A new connection here will be established (it does not disconnect old connections)
print(...) -- this will print for every new and pre-existing connection that's been created
end)
MainFrame.PassButton.MouseButton1Click:Connect(function(plr, ...)
print(...)
end)
end)
something like that and so on and so forth when i need to add the rest but then when do i use this?
local oldEvent = nil
Event:Connect(function()
if oldEvent then
oldEvent:Disconnect() -- Disconnect the old event so it doesn't fire
end
oldEvent = SecondaryEvent:Connect(function(...) -- Only the most recent connection will fire
print(...)
end)
end)
local mainExit = nil
local mainPass = nil
ReplicatedStorage.Remotes.UIRemotes.UI_BoardStart.OnClientEvent:Connect(function(TeamName, ...) -- Every time this event fires
if mainExit then mainExit:Disconnect() end
if mainPass then mainPass:Disconnect() end
-- so and so forth
mainFrame = MainFrame.Exit.MouseButton1Click:Conect(function(plr, ...) -- A new connection here will be established (it does not disconnect old connections)
print(...) -- this will print for every new and pre-existing connection that's been created
end)
mainPass = MainFrame.PassButton.MouseButton1Click:Connect(function(plr, ...)
print(...)
end)
end)
Now, since you have a lot of connections, it may look messy with a ton of variables, so you could also try something like this:
local screenEvents = {}
local function addConnection(connection: RBXScriptConnection)
table.insert(screenEvents, connection) -- add the connection to the table
end
local function removeOldConnections()
for _, connection in screenEvents do connection:Disconnect() end -- disconnect all the connections
table.clear(screenEvents) -- clear the table of old events
end
-- Back to your main RemoteEvent
ReplicatedStorage.Remotes.UIRemotes.UI_BoardStart.OnClientEvent:Connect(function(TeamName, ...) -- Every time this event fires
removeOldConnections() -- clear any old connections that still exist when the connection runs
addConnection ( -- adds the connection to the table
MainFrame.Exit.MouseButton1Click:Conect(function(plr, ...) -- A new connection here will be established (it does not disconnect old connections)
print(...) -- this will print for every new and pre-existing connection that's been created
end)
)
addConnection (
MainFrame.PassButton.MouseButton1Click:Connect(function(plr, ...)
print(...)
end)
)
end)
Now every time the event runs, it will call the function to disconnect and remove old events and add the new connections to the table so they can be cleared later.