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

Trying to make a cash giver that only works once per person. Can anyone help me?

Hi, I'm trying to make a cash giver for advanced tycoon but I need it to only work once per person so if I use it another person can use it right after but I can't. Sorry if that's confusing help appreciate! Here is the script I have right now what should I change I'm a beginner scripter local ting = 0 --debouncer

function onTouched(hit)

if ting == 0 then --debounce check
ting = 1 --activate debounce
check = hit.Parent:FindFirstChild("Humanoid") --Find the human that touched the button

if check ~= nil then --If a human is found, then

	local user = game.Players:GetPlayerFromCharacter(hit.Parent) --get player from touching human
	local stats = user:findFirstChild("leaderstats") --Find moneyholder

	if stats ~= nil then --If moneyholder exists then
		local cash = stats:findFirstChild("Cash") --Get money
		cash.Value  = cash.Value +100 --increase amount of money by the number displayed here (5000)
	 wait(0)
	  --wait-time before button works again
	end

end

ting = 0 --remove debounce
end

end

script.Parent.Touched:connect(onTouched)

3 comments

0

instead of "ting" as an int you would use ting as a table and use the player's name or userId as the key and the number (0 or 1) as the value

0

im sorry but that made no sense to me im a beginner scripter.

0

Why are you making "ting" an int and not a boolean? This just wastes memory(although at your level you shouldn't worry about that stuff yet).

1 answer

local users = {} --Empty table
local debounce = false --A simpler debounce, in my opinion ;)
function onTouched(hit)

	if not debounce then --If debounce is false...
		debounce = true --Debounce!
		hum = hit.Parent:FindFirstChild("Humanoid") --Find the human that touched the button

		if check then --If there's a humanoid

			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr then --If a player touched it,
				local found
				for i, plr_name in pairs(users) do --Loops through the users table
					if plr_name == plr.Name then --If the entry matches the player's name,
						found = true --They've used it
						break --End the loop
					end
				end
				local stats = plr:FindFirstChild("leaderstats")
				if stats and not found then --If they weren't found in the table
					table.insert(users, plr.Name) --Adds the player's name to the users table
					local cash = stats:FindFirstChild("Cash")
					if cash then
					
						cash.Value  = cash.Value + 100 --increase amount of money by the number displayed here (5000)
						wait(0)
					end
				end
			end
		end
		debounce = false
	end
end

If there's something you didn't understand, let me know!

1 comment

0

instead of just giving the code right away, tell them how they'd approach the problem and what kind of functions to use. otherwise they'll just keep coming here expecting to be spoonfed.

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