I'm trying to make a game where the point of the gameplay is to collect batteries (to power up a machine), the problem is my script only works once, as in once someone collects it, no one can collect it anymore. Is there any way I can improve this script so that multiple people can collect it also?
local db = true script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil then if db == true then db = false local player = game.Players:GetPlayerFromCharacter(hit.Parent) player.leaderstats.Batteries.Value = player.leaderstats.Batteries.Value + 1 wait(5) local db = true end end end)
thanks
Here is a trick that works for me and is a multiple people debounce:
local ignoredlist = {} script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then -- you dont have to do ~= nil you can just check if it exists if not ignoredlist[hit.Parent] then -- checks if the character is not debounced ignoredlist[hit.Parent] = true -- makes the debounce on character true (and also adds character to the table) local player = game.Players:GetPlayerFromCharacter(hit.Parent) player.leaderstats.Batteries.Value = player.leaderstats.Batteries.Value + 1 wait(5) ignoredlist[hit.Parent] = false -- makes the debounce on character false again end end end)
The first script that lilzy7 gave is excellent, i have an easier version if you want, you can add a boolean value like called ''hasTouchedThePart'' and when he touches it it sets permanent to true, and then you make an if statement like
if plr.hasTouchedThePart == false then -- proceed or not. end
Both works, even tho lilzy7`s script is more usefull i gave u anwyay another one to gather yourself some ideas for further projects.