Why is :Destroy not working?

I am currently trying to delete an object on the client side. When I delete it with :Destroy(), it freezes in place. When I try to put it in a different service, the same thing happens. Here is my script:

local Player = script.Parent.Parent
local Character = Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local value = game.Workspace.Cosmic
local Cvalue = value.Value

local positionHistory = {}
local delayTime = 1 -- in seconds
local updateInterval = 0.01 -- lower = smoother

local maxHistoryLength = math.ceil(delayTime / updateInterval)

value:GetPropertyChangedSignal("Value"):Connect(function()
	Cvalue = value.Value
	local clone = game.ReplicatedStorage.Rig:Clone()
	clone.Parent = workspace
	if Cvalue == true then
		while Cvalue do
			table.insert(positionHistory, Root.CFrame)
			if #positionHistory > maxHistoryLength then
			clone.HumanoidRootPart.CFrame = positionHistory[1]
			table.remove(positionHistory, 1)
		end
		task.wait(updateInterval)
		end
	else
	clone.Parent = game.ReplicatedStorage
	clone:Destroy()
	end
end)

Here is the script for the part intended to delete the clone:

script.Parent.Transparency = 1

local value = game.Workspace.Cosmic
local End = script.Parent

End.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		value.Value = false
	end
end)

Lastly, here is a video of what happens.


I want it so that the rig disappears when I touch the transparent red block rather than just stop.

when doing

local a = valueObject.Value

a won’t change, as you’re storing its current value in a variable
so when doing

while Cvalue do
end

its the same as

while true do
end

You’ll have to constantly check for value.Value like

while Cvalue.Value do 
end

I change Cvalue whenever the function is run and also Cvalue is the value itself not the bool value which is value.

did you even read what i wrote?

while value.Value do
end

not like it matters what the variable name is, im not gonna write full code for you

I did the change you said to do and nothing changed. The rig still stops and doesn’t disappear.

local Player = script.Parent.Parent
local Character = Player.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local value = game.Workspace.Cosmic
local Cvalue = value.Value

local positionHistory = {}
local delayTime = 1 -- in seconds
local updateInterval = 0.01 -- lower = smoother

local maxHistoryLength = math.ceil(delayTime / updateInterval)

value:GetPropertyChangedSignal("Value"):Connect(function()
	Cvalue = value.Value
	local clone = game.ReplicatedStorage.Rig:Clone()
	clone.Parent = workspace
	if Cvalue == true then
		while value.Value do
			table.insert(positionHistory, Root.CFrame)
			if #positionHistory > maxHistoryLength then
				clone.HumanoidRootPart.CFrame = positionHistory[1]
				table.remove(positionHistory, 1)
			end
			task.wait(updateInterval)
		end
		clone:Destroy()
	else
		clone:Destroy()
	end
end)
1 Like

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