How Do I make a script stop working for a Specific Player?

I’m trying to make a script where the player that touches a part get his leaderstats changed, but I’m struggling how could I make the same player dont get the score from the same part, I even tryed localscripts and creating ObjectValues but nothing seems to work.

Here’s the server script:

local part = script.Parent
local plrDeb = false


part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum  then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local PlrsTouched = part.PlayersTouched
		local PlayerVal = Instance.new("ObjectValue")
		PlayerVal.Parent = PlrsTouched
		PlayerVal.Value = player
		if player ~= PlayerVal and plrDeb == false then
			local HeighValue = player:WaitForChild("leaderstats"):WaitForChild("Heigh")
			HeighValue.Value = HeighValue.Value + 0.5
			plrDeb = true
			wait(0.01)
			plrDeb = false
		end
		
	end
end)

and here is the LocalScript:

local part = script.Parent

part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
		part.ScoreGivingScript.Enabled = false
	end
end)

If anyone help me fixing the issue I will be grateful!

Hey! This could be done utilizing a table, which would allow you to track which players have and have not interfaced with the part. Below is an example.

local Part = script.Parent

local TouchedPart = {}
Part.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
	if hum then
		if not table.find(TouchedPart,hum) then
			table.insert(TouchedPart,hum)
			-- add in leaderstats here
		end
	end
end)

Note: the above code is an example, and I do not recommend using it without modifications, as it is memory-leak prone (it doesn’t clean the table upon humanoid destruction, etc).

2 Likes

Tysm bro! You fixed the issue!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.