How would I send the player back to the beginning?

Im trying to make a ghost that follows the player but currently the ghost does not want to teleport the player back to the start. This is my current script:

NOTE: The ghost is like a full NPC and so far when I touch it, my character does not go back to Stage 0

DB = false

for _,Part in ipairs(script.Parent:GetDescendants()) do
	if Part:IsA("BasePart") then
		Part.Touched:Connect(function(hit)
			if not DB then
				DB = true

				local PossiblePlayer = game.Players:GetPlayerFromCharacter(hit.Parent)

				if PossiblePlayer then
					local Character = hit.Parent
					Character.PrimaryPart:MoveTo(workspace.Stage.Stages:FindFirstChild("0").Position)
					task.wait(5)
					DB = false
				end
			end
		end)
	end
end
1 Like

I’m unable to test any scripts right now, but please try this and see if it works out for you, if it doesn’t, try checking the console for errors and I will help you out:

-- These are the settings for the script
local Players = game:GetService("Players")
local PresetLocation = Vector3.new(0, 0, 0)
local DebounceDelay = 5

-- This function gets the outermost parent of a part
local function GetOutermostParent(part)
	local parent = part.Parent
	while parent.Parent ~= workspace do
		parent = parent.Parent
	end
	return parent
end

-- This function is called when a part is touched
local function OnTouched(hitPart)
	-- Ignore hits that are not descendants of the workspace
	if not hitPart:IsDescendantOf(workspace) then
		return
	end
	-- Get the player from the hit part's character
	local playerCharacter = GetOutermostParent(hitPart)
	local player = Players:GetPlayerFromCharacter(playerCharacter)
	if not player then
		return
	end

	-- Teleport the player to the preset location
	playerCharacter:SetPrimaryPartCFrame(CFrame.new(PresetLocation))
end

-- This loops through all the BaseParts in the parent and connects the OnTouched function to them
for _, part in ipairs(script.Parent:GetDescendants()) do
	if part:IsA("BasePart") then
		local debounceTimer = false
		part.Touched:Connect(function()
			-- Ignore hits that are too close together in time
			if debounceTimer then
				return
			end

			debounceTimer = true
			OnTouched(part)
			task.delay(DebounceDelay, function()
				debounceTimer = false
			end)
		end)
	end
end

1 Like

The script doesn’t seem to work, unless I got something wrong:

local PresetLocation = workspace.Stage.Stages:FindFirstChild("0").CFrame
local DebounceDelay = 5
2 Likes

Have you seen any errors on the output/console?

Yeah so moving the primary part doesn’t work.
Try this instead.

DB = false

for _,Part in ipairs(script.Parent:GetDescendants()) do
	if Part:IsA("BasePart") then
		Part.Touched:Connect(function(hit)
			if not DB then
				DB = true

				local PossiblePlayer = game.Players:GetPlayerFromCharacter(hit.Parent)

				if PossiblePlayer then
					local Character = hit.Parent
					Character:MoveTo(workspace.Stage.Stages:FindFirstChild("0").Position)
					task.wait(5)
					DB = false
				end
			end
		end)
	end
end
1 Like

Also:

1 Like

Ill check to see if this will handle the issue!

You can try using Character:PivotTo(CFrame)
Example:
Character:PivotTo(workspace.Stage.Stages:FindFirstChild(“0”).CFrame)

:PivotTo

I’ve attempted using PivotTo and it seems that it also doesnt want to teleport the player back. Interesting.

Weird. Did you check if the code before the teleporting actually runs? (By using print() )
If it’s running, also try printing Character.Name and Character.Parent, maybe something is wrong with the Character.

Actually, it could be because of the debounce. If the ghost touches the ground, it will count as touching a part and starts the debounce. Like that, the script won’t run again, since you never set debounce to false again if PossiblePlayer didn’t exist.

You forgot something, Model must have Variable, which contains current pivot of the character, like local cp = Character:GetPivot(). And only after that, you can set cp aka currentpivot to the position already.

1 Like

Im confused, how would I do this in the script?

Maybe first check if the hit object is a player before setting the debounce to true. I’m not sure if this will crash the script, but if it does try adding wait() in the script.

So this?

DB = false

for _,Part in ipairs(script.Parent:GetDescendants()) do
	if Part:IsA("BasePart") then
		Part.Touched:Connect(function(hit)
			if not DB then
				DB = true
				if game.Players:GetPlayerFromCharacter(hit.Parent) then
					local PossiblePlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
					
					if PossiblePlayer then
						local Character = hit.Parent
						Character:PivotTo(workspace.Stage.Stages:FindFirstChild("0").CFrame)
						task.wait(5)
						DB = false		
					end
				end
			end
		end)
	end
end

Did it work? I think you need to remove this:

and instead add it to

The script might crash though, so maybe you can add wait().

IT WORKS! But it seems the bomb script I have thinks I touched the last stage? Im unsure of this now…

What I mean by bomb may seem a bit hard to understand but in other-words:

In other words if you head to my game and press the bobm, button. You’ll see.

bobm: being like a challenge thingy. It’s a lot to cover for such a issue but the main problem can be seen when playing the game

link: MONUMENTAL ABYSS - Roblox

EDIT: it works!

I’m not sure, but hopefully you can find your solution. Maybe you can show us your other script?

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