Checkpoint working even after deleted

  1. What do I want to achieve?
    I want a checkpoint that if deleted will stop working and the player will spawn back at the spawn again

  2. What is the issue?
    even when the part WITH the script is deleted the player still spawns at the same position

  3. What solutions have you tried so far?
    I could not find any solutions

local spawn = script.Parent
spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.CharacterAdded:connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = script.Parent.CFrame + Vector3.new(0, 4, 0)
		end)
	end
end)

Pretty sure you have to disconnect your event.

local spawn = script.Parent
local connection 
connection = spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.CharacterAdded:connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = script.Parent.CFrame + Vector3.new(0, 4, 0)
                connection:Disconnect()
		end)
	end
end)

I’m not sure when you want the script to stop working so you’ll have to move the connection:Disconnect() around.

1 Like

Destroying a script on the client doesn’t stop it from running in the server, it’ll continue running. You should try .Disabled instead or disconnect like @1000knives said.

1 Like

Hey there, thanks for the info but even with disconnect it wont work. I want to disconnect it when the parent of the script is nil

local spawn = script.Parent
local connection

connection = spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		player.CharacterAdded:connect(function(character)
			wait()
			if script.Parent then
				character:WaitForChild("HumanoidRootPart").CFrame = script.Parent.CFrame + Vector3.new(0, 4, 0)
			else
				connection:Disconnect()
			end
		end)
	end
end)

Have you tried taking @Winbloo ‘s first option as to disabling the script instead of deleting it?

In addition to my reply:

Even parenting to nil doesn’t stop the script from running with it’s original parent(s), etc.