Greetings, this Script is supposed to teleport a Player after dying to the RespawnPart. Unfortunately that does not work and I have no idea why. It checks for the creator tag because of a Sword Fighting system. I have checked if the tag existed and it indeed did.
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
repeat wait() until Player:HasAppearanceLoaded()
if Character:FindFirstChild("creator") then
Character:SetPrimaryPartCFrame(game.Workspace.RespawnPart.CFrame)
end
end)
end)
The if statement is likely returning as false, which would have the code inside it ignored. You can probably get the humanoid, and Use the Died event to check if they died.
Well, looking at your code, you are checking if the character has the creator Item, are you sure you are looking in the correct place, The creator Instance is usually placed inside the Humanoid, and that may be why its not working.
That is correct, the other script finds this part and adds the points to a database. Just like a basic sword fighting system uses the creator tags inside of the characters.
Its Actually Inside the Characters Humanoid which is a child of the character, and not inside the Character Directly, you need to look inside the Humanoid. or do a recursive search. Like so:
Character:FindFirstChild("creator", true) -- the true makes it so it looks through the Descendants of the Character (its children)
local players = game:GetService("Players")
local respawnpart = game:GetService("Workspace"):WaitForChild("RespawnPart")
players.LocalPlayer.CharacterAdded:Connect(function()
repeat wait() until players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
pcall(function()
print("ADDED")
if players.LocalPlayer.Character:FindFirstChild("creator") then
players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame = respawnpart.CFrame
else
print("NOT FOUND")
end
end)
end)
You could either switch to Deferred SignalBehavior under workspace or use task.defer. I commonly use this line when parenting Characters:
task.defer(workspace.PivotTo, Character, CFrame)
You should also get rid of the repeat line. Not only does it not really help but there’s a Player.CharacterAppearanceLoaded even that you can use instead of polling.
Moving the character right after spawn is a little finicky because the default code also sets the CFrame of the character.
I did some experimenting and found this was the best way to move the character without the camera flashing and the character being always moved:
-- Wait for HRP to be created and added to character
local hrp = character:WaitForChild("HumanoidRootPart")
-- Wait for HRP to be moved to Workspace along with the character
while not hrp:IsDescendantOf(Workspace) do
hrp.AncestryChanged:Wait()
end
-- Wait one cycle to avoid race condition
task.wait()
-- Set CFrame
hrp.CFrame = newCFrame
Adding that to your code, it would look something like this:
local function onCharacterAdded(Character)
local hrp = Character:WaitForChild("HumanoidRootPart")
if Character:FindFirstChild("creator") then
while not hrp:IsDescendantOf(Workspace) do
hrp.AncestryChanged:Wait()
end
task.wait()
hrp.CFrame = game.Workspace.RespawnPart.CFrame
else
warn("Did not find creator ValueInstance, did not set character CFrame!")
end
end
local respawnpart = game:GetService("Workspace"):WaitForChild("RespawnPart")
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
if Character:FindFirstChild("creator", true) then
task.defer(workspace.PivotTo, Character, CFrame)
Player.Character:WaitForChild("HumanoidRootPart").CFrame = respawnpart.CFrame
end
end)
end)