Script destroying part, part still stays after both on server and client

I have a script to parent checkpoint triggers to the workspace, and when they are touched(through magnitude), and destroys them. But the trigger isnt destroyed. after some debugging, the script thinks the part is destroyed. Hers the script:

for i,v:BasePart in pairs(chapobj.checkpointtriggers:GetChildren()) do
		v.Parent = chapobj.wspacefolder.checkpointtriggers
		local connection = nil
		connection = RunService.Heartbeat:Connect(function()
			print("updating "..tostring(math.floor((v.Position - Character.PrimaryPart.Position).Magnitude)))
			if math.floor((v.Position - Character.PrimaryPart.Position).Magnitude) <= 7 then
				print("Checkpoint Updated To, "..v.Name)
				chapobj.currentcheck.Value = tonumber(v.Name)
				updateChecks(chapter,chapobj.currentcheck.Value)
				v:Destroy()
				connection:Disconnect()
			end
		end)
	end

Thanks in advance!

I’ve had a similar problem like this in other programming languages, and I know how to fix them, although I’m not sure if it works with lua. So try this:

for i,v:BasePart in pairs(chapobj.checkpointtriggers:GetChildren()) do
	local v2 = v
	v2.Parent = chapobj.wspacefolder.checkpointtriggers
	local connection = nil
	connection = RunService.Heartbeat:Connect(function()
		print("updating "..tostring(math.floor((v2.Position - Character.PrimaryPart.Position).Magnitude)))
		if math.floor((v2.Position - Character.PrimaryPart.Position).Magnitude) <= 7 then
			print("Checkpoint Updated To, "..v2.Name)
			chapobj.currentcheck.Value = tonumber(v2.Name)
			updateChecks(chapter,chapobj.currentcheck.Value)
			v2:Destroy()
			connection:Disconnect()
		end
	end)
end
1 Like

Nope, still dosnt work. Heres the vid:
robloxapp-20230622-1252102.wmv (1.5 MB)

@Axominock

Nvermind fixed it! Turns out v was refeing to a cloned one.

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