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 5 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:

01local checkFolder = game.Workspace.checkpoints
02 
03game.Players.PlayerAdded:Connect(function(plr)
04    local leaderstats = Instance.new("Folder")
05    leaderstats.Name = "leaderstats"
06    leaderstats.Parent = plr
07 
08    local checkpoints = Instance.new("IntValue")
09    checkpoints.Name = "Stages"
10    checkpoints.Parent = leaderstats
11 
12    for i,v in pairs(checkFolder:GetChildren()) do
13        if v:IsA("Part") then
14            v.Touched:Connect(function(plr)
15                if i == 1 then
View all 24 lines...
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 — 5y
0
Yes, but i want to keep it in leaderstats. Any way to help? kingblaze_1000 359 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 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:

01local folder = game.Workspace.Folder
02 
03local playerpositions = {}
04local spawnpoint = game.Workspace.SpawnPoint
05 
06for i, v in next, folder:GetChildren() do
07    if v:IsA("BasePart") then
08        v.Touched:Connect(function(hit)
09            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
10 
11            if plr == nil then return end
12 
13            playerpositions[plr] = v.Position
14        end)
15    end
View all 31 lines...

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 — 5y
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 — 5y
Ad

Answer this question