Function only work once

how can i make a function only fire once like this script

i wrote this script but i want the value of the ‘‘GemsCollected’’ to go up only once when touched can anyone help?

Emerald.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then
		
		Emerald.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1
		
	end
end)
3 Likes

why did you delete it and also here is my full code:

local Emerald = script.Parent.GemModel_1.Emerald
local Amber = script.Parent.GemModel_2.Amber
local Diamond = script.Parent.GemModel_3.Diamond
local Ruby = script.Parent.GemModel_4.Ruby
local Sapphire = script.Parent.GemModel_5.Sapphire

local GameStarted = script.Parent.GameStarted
local GemsCollected = script.Parent.GemsCollected

Emerald.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then
		
		Emerald.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1
		
	end
end)

Amber.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then

		Amber.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1

	end
end)

Diamond.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then

		Diamond.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1

	end
end)

Ruby.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then

		Ruby.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1

	end
end)

Sapphire.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then

		Sapphire.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1

	end
end)

if GemsCollected.Value == 5 and GameStarted.Value == true then
	
	GemsCollected.Value = 0
	GameStarted.Value = false
	
end
2 Likes
local hasBeenTouched = false

Emerald.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true and not hasBeenTouched then
        -- put your full code here
        hasBeenTouched = true
    end
end)

-- i don't know this will work or not
2 Likes

Emerald.Touched:Once(function(hit)

6 Likes

Disable can touch on the part afterwards

1 Like

just use RBXScriptSignal:Once() which connects a listener to the event and then disconnects it automatically after it is fired.

now if you don’t want to disconnect it and maybe have like a cooldown and then the player should be able to reclaim a gem you’d add a debounce:

local debounce = false
Emerald.Touched:Connect(function(hit)
 if debounce then return end
 debounce = true
--prev code

 task.wait(YOUR COOLDOWN)
 debounce = false
end)
1 Like
local connection = nil

connection = Emerald.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then
		
		Emerald.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1
		connection:Disconnect
	end
end)
1 Like

that’s once() with extra steps.

If you want to make it impossible to touch the part do this :

Emerald.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and GameStarted.Value == true then
		
		Emerald.Transparency = 1
		GemsCollected.Value = GemsCollected.Value + 1
		Emerald.CanCollide = false
		Emerald.CanTouch = false

	end
end)

By doing this, you can’t touch it anymore, not even collide with it, and the value will be increased by only 1.

1 Like

I find it funny how easy this post is, so much so that we all just gathered here to give like 5 unique solutions to this problem :sob: maybe he should‘ve searched the forum a little harder…

3 Likes

I got this thing before, instead of

Use

if hit:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid")

means if it finds a model in it’s parents and a humaoid in it, then execute a function because hit can also be an accessory so it will but out with an error.

Once wont work properly. If it touches a part, it will disconnect even if that part isnt a character.

GetTouchingParts exists
Also you can use an if statement
Disable canTouch
Making the script Enabled = false

Also pretty nieshe you can use a list of players that has collected it to allow the player to collect it once.

Thanks this worked, and thanks to everyone who replied!

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