Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I make this script so that people can collect something, and they cant collect at again?

Asked by 3 years ago
Edited 3 years ago

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

2 answers

Log in to vote
0
Answered by
lilzy7 68
3 years ago
Edited 3 years ago

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)

0
I tried it and it still increases the value even after you grabbed it coolmario098kirby 5 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

Answer this question