Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

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.

0

Yes, but i want to keep it in leaderstats. Any way to help?

1 answer

✓ Accepted answer

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!

2 comments

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

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.