Mesh is cloned more than once when i don't want it to

Not sure if this is a bug or my code. When I reload the first time, the Mag is cloned only once, but when I reload the second time two Mags falls out…? The number of Cloned Mag that falls out depends on the number of times you have reloaded. This function is called by ContextActionService.

local gun = script.Parent
local CAS = game:GetService("ContextActionService")
local Debris = game:GetService("Debris")

--//States
local aiming = false
local shooting = false
local reloading = false
local canshoot = true

--//Parts of gun
local Mag = gun:WaitForChild("Mag")

--//Load Animations
local Reload = Animations:WaitForChild("Reload")
local ReloadAnim = Humanoid:LoadAnimation(Reload)

--//Reload
local function Reload(actionName, InputState, InputObj)
	if InputState == Enum.UserInputState.Begin then
	if reloading then return end
	shooting = false
    canshoot = false
	reloading = true
	AimAnim:Stop()
	
    ReloadAnim:GetMarkerReachedSignal("MagReloadStart"):Connect(function()
    Mag.Transparency = 1
    local prop = Mag:Clone()
    prop.Transparency = 0
    local children = prop:GetChildren()
    for i = 1, #children do
	children[i]:Destroy()
    end
    prop.CanCollide = true
    prop.Parent = workspace
    Debris:AddItem(prop)
    end)
    
    ReloadAnim:GetMarkerReachedSignal("MagReloadEnd"):Connect(function()
	Mag.Transparency = 0
    end)

    ReloadAnim:Play()
	wait(ReloadAnim.Length)
	reloading = false
	canshoot = true
	end
end

--//Gun Equipped
gun.Equipped:Connect(function()

--//Convert Weld to Motor6D
event:FireServer(gun.Name, true)--(gunName, Equipping?)

CAS:BindAction("Reload", Reload, false, Enum.KeyCode.R)
end)

Can I see a GIF of the issue please?

Sure,
https://gyazo.com/7928775f8acfb332b7b2f9db75eaf609

The code that you provided is limited, can you provide more, such as variables, etc…?

So the mag is the part connected to the gun, and the prop is the part thats gonna fall is that correct?

That’s correct, im going to update the code in a sec.

Oh, I can see that These functions

ReloadAnim:GetMarkerReachedSignal(“MagReloadStart”):Connect(function()
Mag.Transparency = 1
local prop = Mag:Clone()
prop.Transparency = 0
local children = prop:GetChildren()
for i = 1, #children do
children[i]:Destroy()
end
prop.CanCollide = true
prop.Parent = workspace
Debris:AddItem(prop)
end)

ReloadAnim:GetMarkerReachedSignal("MagReloadEnd"):Connect(function()
Mag.Transparency = 0
end)

would stack, thats why its doing the same thing over and over again.

You should move these outside of the reload function:

ReloadAnim:GetMarkerReachedSignal("MagReloadStart"):Connect(function()
    Mag.Transparency = 1
    local prop = Mag:Clone()
    prop.Transparency = 0
    local children = prop:GetChildren()
    for i = 1, #children do
	children[i]:Destroy()
    end
    prop.CanCollide = true
    prop.Parent = workspace
    Debris:AddItem(prop)
    end)
    
    ReloadAnim:GetMarkerReachedSignal("MagReloadEnd"):Connect(function()
	Mag.Transparency = 0
    end)
2 Likes

Thanks! didn’t realize I was creating multiple events.

No problem, that happens many times, just make sure, do not put events into functions unless the function would only be called once.

1 Like

Depending on the scenario, I would disagree with you that putting events in functions shouldn’t be done. On that note, there is also the option of calling disconnect on an event that you no longer need.

No, if it disconnects, future function calls won’t work.

What are you talking about?

local eventSignal = Instance.new("BindableEvent")

local function methodABTest()
    local connection do
        connection = eventSignal.Event:Connect(function ()
            print("Hello, world")
        end)
    end

    event:Fire()
    connection:Disconnect()
end

Yes they will.

What about stop complexing it for that, just move this out of the function and its workin g.

This is not complicating the example.

I’m showing you an example of disconnecting an event. Moving the event connection out of the function does help for readability and depending on the way your code is structured, that may be the option you want to go for.

If you’re connecting functions to events inside a function call, then you also need to make sure to disconnect them when you don’t need them. In fact, this goes with any signal, regardless of where you connect it.

What you said is false. This is not a complicated example nor will functions stop working just because a event disconnection is called.