A Probably "Optimised" Checkpoint Script For Obbies!

Hey, atleast it works!.. Hopefully… right guys and girls?
I have tested this code myself, and found no issues. I even put it under a stress test just to be sure, works perfectly as it should.

Overview of this code:

  • I am satisfied with this code, I don’t know if other people would be.
  • Good as is, using collection service, you can have as much as spawnpoints as you want!
  • Checking if the part youre touching IS a spawn location, and not a random part.
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local olp = OverlapParams.new()
local currentCheckpoint

for i, v in CollectionService:GetTagged("Checkpoint") do
	olp.FilterDescendantsInstances = {v}
	
	RunService.Heartbeat:Connect(function()
		local parts = workspace:GetPartsInPart(v, olp)
		
		for _, part in parts do
			local Player = Players:GetPlayerFromCharacter(part.Parent)
			
			if not Player then return end
			
			currentCheckpoint = v
			currentCheckpoint.Enabled = true
			Player.RespawnLocation = currentCheckpoint
			if v ~= currentCheckpoint and v ~= Player.RespawnLocation then
				v.Enabled = false
			end
		end
	end)
end

I know I’m gonna get a lot of hate comments saying “USE .TOUCHED!!!”

…yeah just use touched, i dont see why you wouldnt

4 Likes

It isn’t very performant. you’re essentially having countless instances run per every physics tick, which can drastically slow performance if handled incorrectly. Generally, this is where many people recommend Touch events.

If you’re concerned about implementing hundreds of .Touched events, try using Humanoid.Touched and check if the objects the character is touching have a special property such as using :HasTag() to check if it’s actually a checkpoint.
If you absolutely need to check Player bounds, I recommend using a custom Event to do so per interaction with the checkpoints. It not only removes the need for a constant verification of “Are they where they are supposed to be”, it radically reduces the amount of resources that the Client/Server needs to use, allowing for scalability on a large scale.

1 Like

Hopefully this one is good aswell, generally performance friendly.

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local currentCheckpoint

for _, v in CollectionService:GetTagged("Checkpoint") do
	if v:IsA("SpawnLocation") then
		v.Touched:Connect(function(hit)
			local Player = Players:GetPlayerFromCharacter(hit.Parent)

			if not Player then return end

			currentCheckpoint = v
			currentCheckpoint.Enabled = true
			Player.RespawnLocation = currentCheckpoint
			if v ~= currentCheckpoint and v ~= Player.RespawnLocation then
				v.Enabled = false
			end
		end)
	end
end

Although simple, it does have a few flaws which I do plan to fix.

1 Like