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
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
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
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.
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.
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