:Destroy() Being Skipped

Making a stand system where when you click a button it spawns a dummy behind you

The problem is whenever the time comes to destroy the dummy after the button is clicked again, the :Destroy() function is completely ignored with no errors.

Its maybe being skipped because the count isn’t equal to 2

but if this was the case it wouldn’t warn “huh”

The issue might be that its only deleting the humanoid go clone.Parent:destroy

Doesn’t work the i removed the dummy’s humanoid before cloning it because i don’t want it to have hp. Its also saying that the parent is workspace

Can you send a video of when the clone is made in game and i runs by destroy in the explorer

A video of the explorer like in the workspace when 2 runs that what i meant

Like this?

The reason why is bc your script is running though the first if statement even though its only meant to be running through the second one just make another if statement instead of elseif

make it

if counted == 2 then
…
end

Each time its adding a new dummy even though you are destroying one

But if this were the case, wouldn’t “Stand is made here” also be printed when count == 2 because its not

Its is printing to It just prints right after

the stand isn’t being made there.

myStand is created before the if statement every single time no matter the count.

even if count was 3 for some reason a new stand would be made.

2 Likes

wait so its being made here?

image

Yeah…

You cloned the stand, which makes a new stand, which is represented with the name Mystand.

2 Likes

image

local Mystand = nil

which can also be written as:

local Mystand

should be written above the event, to act as a global variable, so it is saved as a variable and can be accessed, even after the event fires multiple times.
Move the three lines of variables (under the event connection) into the if statement, (under the count == 1 “block”, block is commonly referred to as a scope). Also remove local from local Mystand = Stand:Clone() which is now inside the if statement, because as stated above, it should be defined before the event connection to be referenced as “a global variable”.

final code (next time upload the code in text, it would be much appreciated):

local Mystand
Event-connection-thing:Connect(function()
    if Count == 1 then
        Mystand = Stand:Clone()
        local Character = player.Character
        local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
        Mystand.Parent = workspace
    the rest of the code...
end)

Reference to @RainingMemory’s messages for describing it.
the rest of the code may remain untouched. Have a good night.

4 Likes

ru using script inside local script ?

yeah so as others said you aren’t tracking the current stand that’s cloned. Heres the example of your script but with the stand being tracked in a variable (using context action service instead of user input service for easier compatibility):

local player = game.Players.LocalPlayer

local rp = game.ReplicatedStorage
local RemoteStand = rp.RemoteStand

local Stand = workspace.Dummy

local CAS = game:GetService("ContextActionService")

local debounce = false
local Summoned = false
local Count = 0

local CurrentStand = nil

local function handleAction(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		if debounce == false then
			debounce = true
			print("Summoned")
			Count += 1
			RemoteStand:FireServer(Summoned, Count)
			debounce = false
			print("Count:", Count)
			if Count == 2 then
				Count = 0
			end
		end
	end
end

local function HandleStands(Summoned,Count)
	if CurrentStand then
		print("Destroying current stand")
		CurrentStand:Destroy()
		CurrentStand = nil
	end

	if Count == 1 then
		local Mystand = Stand:Clone()
		local Character = player.Character
		local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
		Mystand.Parent = game.Workspace
		Mystand.Name = "Cloned"
		CurrentStand = Mystand
		print("new stand")
	end
end

CAS:BindAction("SummonStand", handleAction, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonX)
RemoteStand.OnClientEvent:Connect(HandleStands)
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.