Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to teleport player to part when they join the game?

Asked by 3 years ago

I asked this question before but it was not answered so I am just going to ask again. I am making an obby and I need checkpoints. I am creating my checkpoints using the character's Torso and the leaderboard stats. If the player's leaderboard "Stage" stat is 1, then when they die, they will be teleported to Checkpoint 1. To do this, I added a script inside each checkpoint and used functions. The problem is, the player isn't being spawned on the checkpoint. The output shows no errors so I don't know what to do. My script is being shown below and help is much appreciated. Thank you in advanced.

01local checkpoint = script.Parent
02 
03checkpoint.Touched:Connect(function(hit)
04    if hit.Parent:FindFirstChild('Humanoid') then
05        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
06        plr.leaderstats.Stage.Value = 1        
07    end
08end)
09 
10game.Players.PlayerAdded:connect(function(plr)
11    plr.CharacterAdded:connect(function(char)
12        if plr.leaderstats.Stage.Value == 1 then
13            char.Torso.CFrame = game.Workspace.Checkpoint1.CFrame + Vector3.new(0, 10, 0)
14        end
15    end)
16end)

2 answers

Log in to vote
0
Answered by
Robowon1 323 Moderation Voter
3 years ago

Use MoveTo and ensure the HumanoidRootPart Exists

example:

1game.Players.PlayerAdded:connect(function(Player)
2    Player.CharacterAdded:connect(function(Character)
3        repeat wait() until Character.HumanoidRootPart
4        Character:MoveTo(workspace.Part.Position + Vector3.new(0,5,0))
5    end)
6end)
Ad
Log in to vote
4
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

The likely reason that your script was not working is because the player's character is not fully loaded.

ServerScriptService

01game.Players.PlayerAdded:Connect(function(player)
02    local ls = Instance.new("Folder")
03    ls.Name = "leaderstats"
04    ls.Parent = player
05 
06    local stage = Instance.new("NumberValue")
07    stage.Name = "Stage"
08    stage.Value = 1 -- or ds3:get(player,'stage')
09    stage.Parent = ls
10 
11 
12    player.CharacterAdded:Connect(function()
13        local part = game.Workspace:FindFirstChild(("Checkpoint%s"):format(tostring(stage.Value)))
14        if not part then print("Part not found") return end -- No part found, can't set CFrame
15        while not player:HasAppearanceLoaded() do wait() end
16        player.Character:SetPrimaryPartCFrame(part.CFrame + Vector3.new(0,3,0))
17    end)
18end)

On line 8 when I mention 'ds3' in a comment, that is my own datastore module. It signifies loading a saved stage value from the DataStoreService or a custom datastore module.

On line 15, a loop is created that waits until the system registers that the character has been fully loaded.

This script also handles the teleportation for all checkpoints rather than having a group of redundant scripts.

In each checkpoint

01local checkpoint = script.Parent
02 
03checkpoint.Touched:Connect(function(hit)
04    local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
05    -- This method has a possibility for fail, but is good enough for your purposes
06    local stageNum,_ = checkpoint.Name:gsub("Checkpoint","") -- replace Checkpoint with ""
07    stageNum = tonumber(stageNum)
08    if plr and stageNum then -- player and stage number found
09        plr.leaderstats.Stage.Value = stageNum
10    end
11end)

As it looks like you are naming each checkpoint 'Checkpoint#' (ie. Checkpoint1), this will get the checkpoint number and set the player's stage number in the leaderstats folder accordingly.

As always, if this solves your problem, make sure to accept the answer to further help others with the same/similar issues.

Answer this question