Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

1 comment

0

I tried it and it still increases the value even after you grabbed it

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.