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

local checkpoint = script.Parent

checkpoint.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        plr.leaderstats.Stage.Value = 1         
    end
end)

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(char)
        if plr.leaderstats.Stage.Value == 1 then
            char.Torso.CFrame = game.Workspace.Checkpoint1.CFrame + Vector3.new(0, 10, 0)
        end
    end)
end)

2 answers

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

Use MoveTo and ensure the HumanoidRootPart Exists

example:

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

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

ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
    local ls = Instance.new("Folder")
    ls.Name = "leaderstats"
    ls.Parent = player

    local stage = Instance.new("NumberValue")
    stage.Name = "Stage"
    stage.Value = 1 -- or ds3:get(player,'stage')
    stage.Parent = ls


    player.CharacterAdded:Connect(function()
        local part = game.Workspace:FindFirstChild(("Checkpoint%s"):format(tostring(stage.Value)))
        if not part then print("Part not found") return end -- No part found, can't set CFrame
        while not player:HasAppearanceLoaded() do wait() end
        player.Character:SetPrimaryPartCFrame(part.CFrame + Vector3.new(0,3,0))
    end)
end)

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

local checkpoint = script.Parent

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

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