Unexpected behavior in script \ Possibly related to :Disconnect()?

I’m working on a PvP game with a friend and currently I’m scripting some of the skills. One of them is this:

The skill spawns 4 fireballs around your character when casted and wherever you click, the fireballs are fired individually toward the point you clicked on. In the video, that is the intended behavior for the skill.

The issue is, after casting it a second time, the fire balls are no longer fired individually but instead in pairs. For reference, here is another video:

The problem gets worse when the skill is casted a third or forth time. On the third it’ll fire 3 fireballs per click, on the fourth all 4 fireballs.

I tried changing the code multiple times, but I couldn’t find a proper fix. The code is a bit messy indeed, but despite re-reading it, the logic seems correct and I don’t understand where that odd behavior comes from.
Here is the section of the script I suspect is causing the problem:

local currentBall = 1
local function fire_Ball(player,mousePos)
	local HRP = player.Character:FindFirstChild("HumanoidRootPart")
	local fb = nil
	local folder = workspace:FindFirstChild(player.Name.."'s Fireballs")--:GetChildren()

		currentBall = 1
		fb = nil

	if currentBall <= #fireballsTab then
		fb = folder["Fireball"..currentBall]
		--local fb = folder:WaitForChild("Fireball",2)

		fb.Anchored = true
		fb.WeldConstraint.Enabled = false
		fb.CFrame = CFrame.new(fb.Position,mousePos.p)

		Services.TS:Create(fb,TweenInfo.new(3),{CFrame = fb.CFrame * CFrame.new(0,0,-70)}):Play()

		fb.Parent = workspace
		currentBall = currentBall + 1
	elseif currentBall > #fireballsTab then
		for i,part in pairs (fireballsTab) do
			fireballsTab[i] = nil
		currentBall = 1
		Data.isSkillOn = false
		print("Skill not on.")
		if HRP:FindFirstChild("FireballMotor") then

And here is the full script, which I suggest reading since it might also be related: https://pastebin.com/nt2mrQUs
Didn’t include it directly in the post because it’s a bit long.
EDIT: turns out it’s a problem related to an event that might be connected multiple times. If someone knows how to handle this problem, please let me know!


Can you try using the skill three times? Does it use three fireballs? You might have connected the OnServerEvent to the same function multiple times.

1 Like

Do you mean, using it, waiting for it to end, then use it and repeat? As I said, yes it does after casting it a third time.

Oh, perhaps. How can I “disconnect” it or avoid it from happening?

You can add a nil variable, then set it as the OnServerEvent connection, like this:

local onServerEvent = nil;
onServerEvent = Data.Mouse_Remote.OnServerEvent:Connect(fire_Ball);

…and when you return the function at the end of the script, add a :Disconnect() on the onServerEvent variable.
Final result:

local onServerEvent = nil;
return function(player)
    Data.currentTime = tick()
    local cdCheck,newTick = CooldownHandler(math.floor(Data.cdTick),Data.skillCD,math.floor(Data.currentTime),script.Name,player)
    if cdCheck then
        Data.cdTick = newTick
        if onServerEvent then
        onServerEvent = Data.Mouse_Remote.OnServerEvent:Connect(fire_Ball)
    elseif  not cdCheck then
        warn("Cooldown not over yet; Only "..math.floor(newTick).." seconds have passed since last cast.")
    end --cd check

…or you can just add a new index in the “Data” dictionary and then use that one to connect the Remote.OnServerEvent event. Your choice.

P.S. Just noticed, the skill looks cool!

1 Like

I’ll try this, but I’m confused about this part:

What do you mean with this? o-o I’m returning nothing at the end.

Thank you so much!! I appreciate it.
Btw, just checked your profile, it says you are Italian too eheh.

Yup! I will continue using english so everyone can understand.


Aren’t you returning this function:

? At least that’s the code inside the pastebin document.
Have you tried the code I sent earlier already? That should fix your problem.

Oh, yea. I’m returning it because it’s inside a module script, since my skills system is uses an object-oriented approach.

I did and it seems to work if I’m the only person in-game. However, if there are multiple people, only one person at a time can use the skill. If others try to use it, they just waste their mana and nothing happens. :weary:

Oh, I see. That’s because you aren’t using multiple scripts but are always requiring the same one. You need to clone the module for each player that wants to use it and then require the clone.

That’s odd? I have another skill, and that works fine, no matter the number of players. It seems to be a problem only related to this skill…

Can I see your other skill? It’s always best to use multiple modules and not require the same one so you don’t encounter these problems. If you used multiple modules you wouldn’t have the issue you’re talking about in this post too.

Sure, I’ll just send the whole file at this point if that’s okay, I’m starting to think this might be a problem related to the entire system.

The other skill is “Cryogenesis” under the Ice category. Everything is in ServerScriptService.

EDIT: by the way sorry for the spaghetti code in some parts, especially in the fireballs skill, i usually clean up everything once I’m done.

Alright, I’ll take a look at it.

1 Like

Ok, I found some issues, but they all come from trying to change how the skills are handled. I recommend rewriting the system and making it compatible with multiple players as the way you designed it makes it look like it’s for a single-player game. There probably are other solutions that don’t require rewriting it, but you’d get confused really quickly and waste much time. Also noticed you use a bunch of unneeded modules. If you need help, just DM me. I can help you directly in Studio too if you need. Buona fortuna! :slightly_smiling_face:

P.S. Of course I mean rewrite how the skills are handled, not the skills itself, they work fine but there also are some changes that need to be made

1 Like

Thank you for much for helping! Might you add me on Discord, since at this point I’m basically stuck? :skull:
My tag is LaggyMess#6411. (your DMs here on the forum are off it seems?)

1 Like