My problem is when a player touch the part it automaticly teleport every players at the 'Respawn' Part,
I just want to teleport the player who touch it
local amount = 250 debounce = true script.Parent.Touched:Connect(function(hit) local player = game.Workspace[hit.Parent] print(player.Name) if player.Character then local stats = player:FindFirstChild("leaderstats") if stats then local gui = player.PlayerGui if gui then if debounce == true then debounce = false stats.Cash.Value = stats.Cash.Value +amount game.Workspace.TrainingMap.EndLavaMap.Sound:Play() player.Character:MoveTo(workspace.Respawn.Position) gui.PlayerFrame.Menu.AddRemove.Text = "+" wait(1) gui.PlayerFrame.Menu.AddRemove.Text = "" debounce = true end end end end end)
A few problems with your script.
Line 5:
local player = game.Workspace[hit.Parent]
When looking for a player its best to do this:
local player = game.Player:FindFirstChild(hit.Parent.Name)
You are looking for the player's name when a player touches it so you can call for it in game.Players
Line 7:
if player.Character then
There is no character in a characters model so this should be removed. It would be best to check if a humanoid touched it to insert this line between line 4 and 5
if hit.Parent:FindFirstChild("Humanoid") then
Line 16:
player.Character:MoveTo(workspace.Respawn.Position)
Dont use :MoveTo() for teleporting players. Use CFrame
hit.Parent.HumanoidRootPart.CFrame = workspace.Respawn.CFrame
So your end result would look like this:
local amount = 250 debounce = true script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game.Player:FindFirstChild(hit.Parent.Name) print(player.Name) local stats = player:FindFirstChild("leaderstats") if stats then local gui = player.PlayerGui if gui then if debounce == true then debounce = false stats.Cash.Value = stats.Cash.Value +amount game.Workspace.TrainingMap.EndLavaMap.Sound:Play() hit.Parent.HumanoidRootPart.CFrame = workspace.Respawn.CFrame gui.PlayerFrame.Menu.AddRemove.Text = "+" wait(1) gui.PlayerFrame.Menu.AddRemove.Text = "" debounce = true end end end end end)