i am not very good at scripting so i need a little help. The item giver i made gives a speedcoil to the person who touched it. But the problem is every time the character touched it, it adds another speed coil to the inventory. things to know --- Speed coil is in server storage
1 | local plr = game.Players.LocalPlayer |
2 | if plr.Backpack:FindFirstChild( 'SpeedCoil' ) or plr.StarterGear:FindFirstChild( 'SpeedCoil' ) or game.Workspace [ plr.name ] :FindFirstChild( 'SpeedCoil' ) then |
3 | game.ServerStorage.SpeedCoil:Destroy() |
4 |
5 | end |
What you are looking for is called a Debounce! As in No bouncing! It's what programmers use to account for the microsopic bounces that happen with buttons, touch evens it game, ect..
The most common way to fix this is simple!
1 | local debounce -- this is what we are using! |
2 | script.Parent.Touched:Connect( function () -- simple example touched function |
3 | if debounce then return end -- this says, If debounce (or "is bouncing") then stop the script. |
4 | debounce = true -- if debounce isnt true then lets make it true |
5 | print "touched" |
6 | wait( 0.5 ) -- cool down of 0.5 seconds between touches! |
7 | debounce = nil -- turn off boucning so it can be used again |
8 | end |