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

Teleport script teleports all players? [Solved]

Hello,

I'm trying to make a script that teleports the player to their current stage. The script does work, but all the methods I've tried teleports all players to their current stage. Not only the one who touched the part! How would I go about fixing this? Please help me!

This is my latest try. Help is appreciated!

local Debounce = false

function TPToCurrentStage(hit)
	local Players = game.Players:GetPlayers()
	for i = 1, #Players do
		local plr = Players
		local StageNumber = Players[hit.Parent].leaderstats.Stage.Value
		local StageString = tostring(StageNumber)
		if StageString == "0" then
			local StageO = game.Workspace.LobbySpawns.Spawn
			Players[hit.Parent.Name].Character.HumanoidRootPart.CFrame = StageO.CFrame + Vector3.new(0,3,0)
		else
			local CurrentStage = game.Workspace.Levels:FindFirstChild(tostring(StageString))
			Players[hit.Parent.Name].Character.HumanoidRootPart.CFrame = CurrentStage.CFrame + Vector3.new(0,3,0)
		end
	end
end

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
		Debounce = true
		TPToCurrentStage(hit)
		wait(.5)
	end
	Debounce = false
end)

1 answer

Nevermind, I managed to fix it. I ended up going for a slightly different approach, which I probably should of in the first place.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Players = game:GetService("Players")
		local Player = Players:FindFirstChild(hit.Parent.Name)
		local StageNumber = Player.leaderstats.Stage.Value
		local StageString = tostring(StageNumber)
		if StageString == "0" then
			local StageO = game.Workspace.LobbySpawns.Spawn
			Player.Character.HumanoidRootPart.CFrame = StageO.CFrame + Vector3.new(0,3,0)
		else
			local CurrentStage = game.Workspace.Levels:FindFirstChild(tostring(StageString))
			Player.Character.HumanoidRootPart.CFrame = CurrentStage.CFrame + Vector3.new(0,3,0)
		end
	end
end)

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