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()
task.spawn(function()
wait(Data.Duration)
currentBall = 1
fb = nil
end)
if currentBall <= #fireballsTab then
fb = folder["Fireball"..currentBall]
--local fb = folder:WaitForChild("Fireball",2)
print(currentBall)
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()
Services.Debris:AddItem(fb,Data.Lifetime)
fb.Parent = workspace
currentBall = currentBall + 1
elseif currentBall > #fireballsTab then
for i,part in pairs (fireballsTab) do
--fireballsTab[i]:Destroy()
fireballsTab[i] = nil
end
currentBall = 1
Data.isSkillOn = false
Services.RS.RemoteForFireball:FireClient(player,Data.isSkillOn)
print("Skill not on.")
if HRP:FindFirstChild("FireballMotor") then
HRP:FindFirstChild("FireballMotor"):Destroy()
end
end
end
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!
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
main(player)
if onServerEvent then
onServerEvent:Disconnect()
end
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
end
…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.
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.
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.
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.
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!
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
Thank you for much for helping! Might you add me on Discord, since at this point I’m basically stuck?
My tag is LaggyMess#6411. (your DMs here on the forum are off it seems?)