Script is being touched once, but activates multiple times?

This is what happens:
I have made a coin collection script, if you touch the coin the coin disappears for the player who touches it (this part is working). It also plays a coin-collect sound (which also works) the problem is that once you touch it, the sound will be played 1-5 times and you will get 1-5 coins, while it is supposed to play once and you are supposed to just get one coin.

Extra
I’m not sure what causes this, because the coin is destroyed once you touch it. I can’t find anything on the developers forum which can help me with this, and I’ve tried to solve this but nothing works.

If you want to see the script or a video where you can see what happens feel free to ask.
I forgot to mention that I’m working with a RemoteEvent so that every player can collect the coin once.

1 Like

Is the sound being looped? I cant think of much otherwise.

2 Likes

No, but I do see that if I get 3 coins from it, the sound also plays three times. When I touch the part once, it just seems to touch it multiple times…

So what I am thinking right now is that maybe my torso touches it once, my left arm and left leg touches it: and so will it be activated three times? I honestly don’t know, but is there a way to prevent that?

Can I see the script? (chars))))

Sure, I’ll add it to the post. give me one moment.

The problem is that all the player parts are touching, you should add check like this:

Part.Touched:Connect(function(hit)

if hit:FindFirstChild("Humanoid")

-- Code here!

end)
1 Like

This error is fixed, so ignore this message.

I have now:
But it still activates multiple times.

local db=true
local remote = game.ReplicatedStorage.CoinCollect

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then 
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr and db then
		db = false
		plr.leaderstats.Coin.Value = plr.leaderstats.Coin.Value + 1
		remote:FireClient(plr, script.Parent)
		db = true 
	end
	end
end)

That script doesn’t destroy the object, so it is very likely that the Touched event will fire multiple times. You want to destroy the object in the block before setting db to true again.

Just destroy the part after the check… It may fix the problem.

Mark me as a solution If its working :slight_smile:

Well it makes the RemoteEvent (CoinCollect) activate, which will activate the local script in the StarterPlayerScripts, and that destroys the part.

Should I make it like this?

if part = destroyed -- or something like that
then db = true

You should not make db=true local

Destroying the part on the client will not work - as your touched event is on the server, and will continue to fire even if the object doesn’t exist on the client. Perform authoritative tasks on the server.

There should also be a db waiting time: wait(?) db = true

2 Likes

oh, I did not know that. What do you recommend me to do now? Because I still want everyone to be able to collect the coin once, and if I make it destroyed in the script in the server, the other players can’t collect it anymore.

After putting a db waiting time, it works now!

What you could do is add a debounce so that it can only run once and then gets destroyed before the debounce ends.

My recommendations:

  1. Set up the touched event on the client, and fire a remote event to the server to tell it when the coin has been touched (Low security). You could use CollectionService to add the coins to so that a single LocalScript can just find all the coins and set up all the touched events in one loop.

  2. Keep a record on the server of who has already been rewarded. You could insert the player object into a table and check if the player is in there or not before awarding them, and then for visual reasons destroy the coin on the client as you are currently doing. (Better security).

edit: Adding arbitrary wait times can help sometimes, but they often just hide the problem and it will still occur in edge cases. I’d recommend one of the above options to make it more failproof against different network speeds and latency.

Hey, thank you for the recommendations! the collectionservice is indeed a great idea, but after putting a waiting time before the db =true it works now too. Thank you anyways!

Edit: okay, well, it seems to work now but I will keep your idea in mind if it stops working or for any other script. Thanks.

Thank you! It works now. Have a nice day and thanks for the help.