Hi all, I am using collecting service to gather all of my checkpoints and cause them to update your progress when they are stepped on (see below) but the function is called too many times when the player makes first contact.

I think this is because it is calling the function for each body part which touches the part rather than just once.

Can anybody help me make this function only be called once per player per checkpoint? Thank you.

local DS = require(game.ServerScriptService.modules.DataStore)
local Tag_Name = "SpawnPoint" --The name of the tag to look for

--TODO: Fix the checkpoint triggering more than once...

local function onTouched(character, stage_number, part)
	local player = game.Players:FindFirstChild(character.Name)
		local current_stage = player.player_stats.Stage.Value
	
	if player and player.RespawnLocation ~= part and current_stage ~= stage_number then
		if current_stage == stage_number-1 or current_stage == stage_number-2 then
			player.RespawnLocation = part
			DS:ChangeStat(player, "Stage", stage_number-current_stage)
		elseif current_stage <= stage_number-3 then
			character.Humanoid.Health = 0
			--TODO: make a GUI popup warning
			warn(player.Name .. " has missed " .. stage_number-current_stage .. " checkpoints")
		end
	end
end


----This finds tag searched for and execute funciton when part is touched----
local collection = game:GetService("CollectionService")
local scope = collection:GetTagged(Tag_Name)

for _,part in pairs(scope) do
	local stage_number = part.stage.Value
	part.Touched:connect(function(hit)
		onTouched(hit.Parent, stage_number, part)
	end)
end

1 comment

0

You should check if the object touching the part is a player by checking for the humanoid. Because that runs whenever it's touched by anything.