Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make an event that is triggered multiple times only trigger once?

Below is a script that is supposed to fire the dest event once the ore durability is 0

if dur.Value-Damage <= 0 then
			updateBar(Maxdur.Value, Maxdur.Value, false)
			game.ReplicatedStorage.Mining.sound:FireServer(sound)
			game.ReplicatedStorage.Mining.sound:FireServer(sound2)
			game.ReplicatedStorage.Mining.drop:FireServer(Tar, despawnTime)
			game.ReplicatedStorage.Mining.dest:FireServer(Tar)

Below is a script that destroys the target ore once the dest event is triggered and everything works fine up to this point.

game.ReplicatedStorage.Mining.dest.OnServerEvent:Connect(function(plr, tar)
		tar:Destroy()

end)

The last script is meant to subtract one from the total ore count, but for some reason, the dest event happens multiple times and it changes the amount of times triggered for every ore that is destroyed. This screws up the total ore count and breaks the game.

game.ReplicatedStorage.Mining.dest.OnServerEvent:Connect (function()
	OreCount = OreCount -1
end)

I think I did what you meant...

local fired = false
if dur.Value-Damage <= 0 and not fired then
			updateBar(Maxdur.Value, Maxdur.Value, false)
			game.ReplicatedStorage.Mining.sound:FireServer(sound)
			game.ReplicatedStorage.Mining.sound:FireServer(sound2)
			game.ReplicatedStorage.Mining.drop:FireServer(Tar, despawnTime)
			game.ReplicatedStorage.Mining.dest:FireServer(Tar)
			fired = true

There are nor errors and it still doesn't work

2 answers

✓ Accepted answer

Use a Boolean value to check if the event has fired or not. For example if you had this event...

local fired = false
if health <= 0 and not fired then
	fired = true
	fireEvent()
end

This will make sure the event can only fire one time since you re setting fired to true once it is triggered one time.

local fired = false
game.ReplicatedStorage.Mining.dest.OnServerEvent:Connect (function()
	if not fired then
	OreCount = OreCount -1
	fired = true
	wait (.00001)
	fired = false
end

I just put it on the other script and it worked I think because event dest was activated multiple times at the exact same time so just offsetting this by a little bit (.00001 sec.) gave the script time to change the fired value. Thank you very much!

This discussion is preserved as it was. New answers, comments, and votes are closed.