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)
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
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
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)
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 maybe he should‘ve searched the forum a little harder…
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.