#RBXScriptConnection help

1 messages · Page 1 of 1 (latest)

lavish grail
#

Client (localscript) inside starter player scripts

local Players = game:GetService("Players")
local player = Players.LocalPlayer


player.CharacterAdded:Connect(function()

  local CrateClient = require(modules:WaitForChild("CrateClient"))
  local fireCrate = events:WaitForChild("FireCrate")
  --A shitload of variables

  local crateconnect = nil
    
  local function firecrateEvent(var, winner, number)
    CrateClient:fireCrate(var, winner, number, crateconnect)
  end
    
  crateconnect = fireCrate.OnClientEvent:Connect(firecrateEvent)

end)

The issue I'm having is that every time I try to play the crate game it will double fire so I assumed it was the :Connect function not disconnecting properly after I finish playing it so I added a disconnect() function inside the module script. The function wont run again after its been disconnected how do I disconnect the function and reconnect it again if I decide to play it again (ofcoarse without resetting my character every time as this is also nested inside the characteradded function aswell).

I will post more code if required

lavish grail
#

i know it looks dumb im pretty new to coding

short yewBOT
#

studio** You are now Level 4! **studio

icy shoal
#

So firstly you don’t need to set connect to the client event

short yewBOT
#

studio** You are now Level 20! **studio

icy shoal
#

Secondly, you can enable and disable your crate game inside the crate game’s module

#

This one should only be responsible for sending data to turn it on and off

#

The double fire doesn’t seem to come from this script either

lavish grail
#

okk

#

the gui button activated:Connect functions are outside the module - i did this because i need everything to work even after the player resets or dies

#

i just dont have the code here i removed it from here because it will look too hard to read

icy shoal
#

I’m assuming you’re using FireClient twice

lavish grail
#

so i believe its double firing because of the nested :Connect functions

player.Characteradded:Connect(function() -- this doesnt disconnect
firecrate.onclientevent:Connect(firecrateevent) -- this fires once first time then twice the second time
end)

icy shoal
#

That could be true yes

#

Why do you need the character added function?

lavish grail
#

so everything loads after the player resets

icy shoal
#

So you’re saving where the UI was when you respawn?

lavish grail
#

theres about 30 more module scripts inside this aswell

#

yes correct

icy shoal
#

Well you can disable reset GUI on death or just set a variable that handles the on and off state

#

CharacterAdded can toggle to on if it’s off

#

Player removal won’t change this state

#

Also that way, you can use

if state then
–– Enable UI
else
–– Disable UI
end
lavish grail
#

i know but it also breaks a bunch of other things such as item placement etc.. im redacting a LOT of code here this game is about 6 months in of development

#

i couldnt put the code here without it being a big read

#

i need the modules to reload a bunch of stuff every time the player resets

#

i have tried disable reset GUI but a lot of stuff does not work correctly

lavish grail
#

it starts stacking the guis ontop of eachother

#

i need it to reset

#

its probably cuz i coded it bad but i cant seem to fix this issues

icy shoal
#

not too sure though

#

I would probably store which UI you have open and then have it open again when your character is added

lavish grail
#

its not just UI its also client placement scripts let me check something real quick i made a backup of my game from earlier im testing

#

so if i hold an object it allows me to place it so the client will render the outline of the object at the mouse position

#

i might have to try something new unless i can get the Rbxscriptconnection to reconnect after it disconnects then im going to have to go a completely different approach

lavish grail
#

@icy shoal what i wanted to do was reconnect a disconnected function. not change days of code because you would of done it differently

icy shoal
#

You don’t have to spend all this time working with a connection that can possibly result in a data leak anyways

#

At least with an on and off switch, you can have more ways to interact with it which opens up the characteradded function and more

icy shoal
#

This solves your “Connection” event from firing after the CharacterAdded did that first

lavish grail
#

hold on i think i might understand let me check if it works.

lavish grail
lavish grail
#

i tried and it still double fires im not sure why

#

tho i appreciate the help btw @icy shoal

lavish grail
#

debounce doesnt work sadly i have to use connection because its the only one that actually stops all errors from showing up