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.
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?
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.
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.
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.
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.
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.