In my game, a proximity prompt fires a bindable event once triggered, and runs some code which pauses a while true do function. Once the code which ran is finished the while true do function continues
The event fires once on the first time around, but once multiple rounds are played the event starts to fire twice
This is my code:
--- PROXIMITY PROMPT CODE
local debounce = false
script.Parent.Triggered:Connect(function(a)
if debounce then return end
debounce = true
warn("Triggered on behalf of "..a.Name)
local replicatedStorage = game:GetService("ReplicatedStorage")
local bindableEvents = replicatedStorage.BindableEvents
local initMeeting = bindableEvents.StartAmongUsMeeting
initMeeting:Fire()
wait(1)
debounce = false
end)
-- GAME HANDLER CODE (not all the code is here)
local originalPositions = {
}
playing = true
local timeForGame = curGameMode.Time
print(curGameMode)
print(curGameMode.Time)
local seconds = 0
local isInMeeting = false
local amountOfFires = 0
bindableEvents.StartAmongUsMeeting.Event:Connect(function()
warn("Recieved event. ")
--wait()
if amountOfFires < 1 then
amountOfFires += 1
warn("Meeting started.")
local alive = {}
warn("Alive table")
warn(alive)
warn("Tagged table")
warn(workspace.tagged:GetChildren())
for i,v in pairs(game.Players:GetPlayers()) do
if workspace.tagged:FindFirstChild(v.Name) then continue end
table.insert(alive,v)
end
-- ETC
isInMeeting = false
amountOfFires = 0
end
end)
while playing do
if isInMeeting then wait() continue end
if seconds < timeForGame then
info.Value = "Time left: ".. SecondsToMinsAndSeconds(timeForGame-seconds)
seconds += 1
wait(1)
else
playing = false
info.Value = "Round over!"
end
end
playing = false
-- ROUND ENDED
soundManagerEvent:FireAllClients("endRound")
Yes, however the parent model of the proximity prompt is always destroyed at the end of each round. Also, I have checked to see if the proximity prompt is being duplicated more than once and it is not.
I wouldn’t recommend putting the script inside of your proximity prompt, try to have a script that handles the prompt somewhere where it doesn’t get destroyed each round
Hey there, I noticed you solved this already but I’ll give you some context behind the reason for the fix:
When you connect to an event, that connection will always exist until you disconnect it. The :Connect() function returns a signal object, which has a function called :Disconnect() that allows you to stop future calls and restores memory that was used by the connection.
The reason why :Once() worked is because it only fires once. It disconnects automatically after being called. But this also means you can only call it once when doing so.
In the future, I will suggest disconnecting any events that you no longer need (example: round-related connections should be disconnected once the round is over).
Yep! You would disconnect it as the first thing and then run your code. Once :Disconnect() is called it wont fire thereafter.
I personally haven’t been using :Once() that often because the cases I run into require the event to fire multiple times, but then should disconnect after say, a round ends.
A good example to use :Once() is when you have a ProximityPrompt that will remove itself after being prompted (given that there are no conditions required to be met upon triggering it).