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

How do i make a player's position change when a part is touched?

Asked by 4 years ago

So i wrote a pretty sensible code to make a checkpoint work when it is stepped on. How do i make it so when you die, it will teleport you to the checkpoint? Here is my attempt:

local checkFolder = game.Workspace.checkpoints

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

    local checkpoints = Instance.new("IntValue")
    checkpoints.Name = "Stages"
    checkpoints.Parent = leaderstats

    for i,v in pairs(checkFolder:GetChildren()) do
        if v:IsA("Part") then
            v.Touched:Connect(function(plr)
                if i == 1 then
                    checkpoints.Value = 1
                    if plr.Parent:WaitForChild("Humanoid").Health == 0 then
                        plr.Parent:WaitForChild("Humanoid").Position = checkFolder.Check1.Position
                    end
                end)
            end
        end
    end
end)
0
Just so you know, one really cheap way to do this (but it works for obbies) is to just use a different team for each stage and use non-neutral spawn locations as checkpoints. f59ph_iv 0 — 4y
0
Yes, but i want to keep it in leaderstats. Any way to help? kingblaze_1000 359 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Your code is almost right. My way to do this would be to create a dictionary of players' spawn positions, then update those when they touch something. Then for every who joins I'd connect to their characteradded and move them there. Example below:

local folder = game.Workspace.Folder

local playerpositions = {}
local spawnpoint = game.Workspace.SpawnPoint

for i, v in next, folder:GetChildren() do
    if v:IsA("BasePart") then
        v.Touched:Connect(function(hit)
            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

            if plr == nil then return end

            playerpositions[plr] = v.Position
        end)
    end
end


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

    local checkpoints = Instance.new("IntValue")
    checkpoints.Name = "Stages"
    checkpoints.Parent = leaderstats

    plr.CharacterAdded:Connect(function(char)
        char:MoveTo(playerpositions[plr] or spawnpoint.Position)
    end)
end)

Reply with further questions,

and please mark as correct if this solution works for you!

0
Don't forget to remove the player's position from the table when they leave the game, or else you'll leak memory. Also, you missed updating checkpoints.Value, maybe just the following after line 13: plr.leaderstats.Stages.Value = i chess123mate 5873 — 4y
0
This doesn't work because when i do this, it completely removes leaderstats as the code for that comes after, when i put it before it still doesn't work. Is there any way to solve this? kingblaze_1000 359 — 4y
Ad

Answer this question