Respawn script teleporting the whole server

So I’ve made a script that is supposed to teleport a player back to their checkpoint in my upcoming Chart Obby. It does that, but it ends up teleporting the whole server back to their checkpoints. I’ve tried a bunch of different code like doing it all in localscripts, but haven’t succeeded yet, and I’m currently out of ideas.

Here is the code:

local Remote = game:GetService('ReplicatedStorage').Events:WaitForChild('Back')

Remote.OnServerEvent:Connect(function(plr)
	local function BackToCheckpoint()
			local checkPoint = game.Workspace.Stages:FindFirstChild(tostring(plr.leaderstats.OnStage.Value))
			local char = game.Workspace:FindFirstChild(plr.Name)
			local checkPosition = checkPoint.Position
			char:MoveTo(checkPosition)
		end
	BackToCheckpoint()
end)
--The remote event fired when a part was touched, which moves them back to their checkpoint.

Here is how it looks ingame:
https://gyazo.com/0a9d68d6112f9a302e6ad535045c88ff.gif

If you need more infomation or have any ideas on what is causing this, please let me know!

It’s likely something wrong with the Touched event, due to how I see OnServerEvent, the Touched Event is client sided, may I see your Touched event code? You likely aren’t verifying who touched the part

Absolutely! Here’s the code from the localscript:

local CollectionService = game:GetService('CollectionService')
local Remote = game:GetService('ReplicatedStorage').Events:WaitForChild('Back')

for _, parts in pairs(CollectionService:GetTagged('TBTC')) do
	parts.Touched:Connect(function()
		Remote:FireServer()
	end)
end

That’s why then, there’s no verification on who touched the part, so if a single player touches it, it’s going to fire it for everyone

I think this should work for you

local Players = game:GetService('Players')
local CollectionService = game:GetService('CollectionService')
local Remote = game:GetService('ReplicatedStorage').Events:WaitForChild('Back')

local player = Players.LocalPlayer

for _, parts in pairs(CollectionService:GetTagged('TBTC')) do
	parts.Touched:Connect(function(hit)
		local character = player.Character
		if not character or not hit:IsDescendantOf(character) then
			return
		end
		Remote:FireServer()
	end)
end

@foundanerror Thanks for indirectly pointing out that I should’ve put player.Character as a variable there haha

1 Like

There is no need to make the function if your gonna put it directly in the ServerEvent. Also, do
local char = player.Character instead

Also do what the person above said

2 Likes

I’ve tried your code, but the same thing happens…

EDIT: stupid me forgot to apply the edits, it works! Thank you so much!

1 Like