Script cloning model multiple times

Hello! this is my first post to the devforum as ive just been made a member which is pretty exciting :slight_smile:

in any case ive run across a strange issue.
im in the process of writing a simple fireball script. right now the script just grabs the player’s hand CFrame and just clones it onto their hand. however, if the script is fired multiple times it clones one more duplicate fireball each time it is used.
eg: first time spawns one, second time spawns two, ect.

here is the code:
relevant code in the player’s localscript:

--the animation has a marker at the midpoint called "Cast", which is the point 
--the fireball should be launched. the marker "End" is simply a marker at the
--last keyframe
uis.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if castingfireball == false then
	if input.KeyCode == keybind then
		castingfireball = true
		animation:Play()
		animation:GetMarkerReachedSignal("Cast"):Connect(function()
			local handcframe = player.Character.RightHand.CFrame
			fireballevent:FireServer(handcframe)
		end)
		animation:GetMarkerReachedSignal("End"):Connect(function()
			animation:Stop()
			castingfireball = false
		end)
	elseif castingfireball == true then return end
	end
end)

and the serverscript that gets fired (as it is right now):

function firespell(player,handcframe)
local thefireball = AbilityAssets.Fireball.Fireball:Clone()
thefireball.Parent = game:GetService("Workspace")
thefireball:SetPrimaryPartCFrame(handcframe)
end
FireballEvent.OnServerEvent:Connect(firespell)

testing with console prints indicated that the server script is actually being fired multiple times, which only confused me even more. the animation plays just fine, and the localscript doesnt run any duplicate lines. so if anyone has any idea what might be causing this i would greatly appreciate any input.

also: sorry if i melted anyone’s eyes with that code. i am still learning so any bad habbits or optimizations anyone notices would also be great to hear about. thanks in advance!

You Can Use Debounce that can allow it to do it and I don’t know if the casting fireball is the debounce or not so I may be wrong but the error may be there your debounce

1 Like

in all likelyhood it has something to do with it (its pretty jank) but i have no clue how it does or how to fix it. ill look at it tho, thank you

bruh i just saw the error here

the server is reading like this:

if castingfireball == false then -- no end = error

if input.KeyCode == keybind then
-- code
elseif castingfireball then return end
end

This is the error lol i just seen it and yeah( oh @BielNS replied first thx man for helping him)

test dat and if it works press dat solution buttons soo other can see epic fix if they have that problem too

1 Like

EPIC FIX 101!
Have a good day if it works

im not sure i quite understand what you mean. the end that goes to that line is just below

elseif castingfireball == true then return end

unless youre meaning something else or im just not seeing it.

1 Like

I see the reason now because the return stops the script instantly but the player is still pressing it mabye I’m wrong.

The reason every click is firing the remote multiple times is because you keep connecting the same event every single time:

animation:GetMarkerReachedSignal("Cast"):Connect(function()
			local handcframe = player.Character.RightHand.CFrame
			fireballevent:FireServer(handcframe)
		end)

What you need to do is either disconnect both GetMarkerReachedSignals once they run or you should put the connections outside the input connection.

Hope this helps.

2 Likes

this worked perfectly. once i added a disconnect line everything was fine. thanks so much!

1 Like

No problem! Glad the issue was solved!

1 Like