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.
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
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