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

How do i make a brick give money when touched but don't give money again until i reset character?

also, how do I make it so it works for 1 player only

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 Coins = stats:findFirstChild("Coins") --Get money
            Coins.Value  = Coins.Value +5 --increase amount of money by the number displayed here (500)
            wait(20) --wait-time before button works again
        end

    end

    ting = 0 --remove debounce
    end

end

script.Parent.Touched:connect(onTouched)

2 answers

I don't exactly understand what you mean by one player, but as for not working till they reset their character that's rather easy. Each time someone dies, the whole model of their character is destroyed and they respawn. We can use this to our advantage. When the script is activated, put some kind of tag onto them, for example, it can just be a simple BoolValue instance, then name it something like "MoneyBrick". Then in the script, if it detects the name of this instance in the player than don't give them any money.

6 comments

0

yeah i wasn't actually understandable but i mean i made it so a player has to wait 20 seconds before beeing able to get money again but if someone gets the money the whole server has to wait 20 seconds to get money again

0

Is that part of the script working?

0

ye but i don't know lua so much, i can fix things but can't make a fully working script without tutorials

0

So are you still having any problems? and if so then what are the problems you're having?

0

i don't know how to make a boolean

0

Instance.new("Boolean", character).Name = "MoneyBrick" --This is also used to any other kind of instance, the second argument is the parent, but you can also set the parent with "instance.Parent = parent" where the newly created instance is saved into the variable "instance"

Okay, so almost an hour of trying to get this to work, I've managed to successfully do it (Could've taken less time but It's currently 4am and I'm super tired). So I previous made the answer without the Timer that you wanted, and I've been working on the Timer. And I finally got it to work. Here's everything you'll need.

You will need a:

  1. Script(x2) Both Their Parent's will be in the part.

  2. A Timer(NumberValue) Which will be stored in the part also. Name it TimeUntilAvailable (Keep it's Value to 0)

  3. A Bool Value (Which will become true if the part is touched.) Name it isTouched.

  4. Another BoolValue (Which will become true if the player has reset, and will be false if they have not reset.) Name it CharacterHasReset

  5. And finally, another Script which will create the CharacterHasReset Value inside of the Character. (Script's Parent will be StarterCharacterScripts)

So here are all the scripts:

Script 1, Timer Script:

local TimeUntilAvailable = script.Parent.TimeUntilAvailable

	while true do
		wait(1)
		if script.Parent.isTouched.Value == true then
			TimeUntilAvailable.Value = TimeUntilAvailable.Value + 1
			if TimeUntilAvailable.Value >= 20 then
				TimeUntilAvailable.Value = 20
			end
		else
			TimeUntilAvailable.Value = 0
	end
end

Script 2, Detecting if the Character touched the Part:

local function onTouched(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		
		local Touched = script.Parent.isTouched
		
		local Character = hit.Parent
		
		local CharacterHasReset = Character.CharacterHasReset
		
		local TimeUntilAvailable = script.Parent.TimeUntilAvailable
	
		local Player = game.Players:GetPlayerFromCharacter(Character)

		local Stats = Player:FindFirstChild("leaderstats")
		
		local Coins = Stats:FindFirstChild("Coins")
		
		if Touched.Value == false and TimeUntilAvailable.Value == 20 and CharacterHasReset.Value == true then
			Coins.Value = Coins.Value + 5
			CharacterHasReset.Value = false
			Touched.Value = true
			
			TimeUntilAvailable:GetPropertyChangedSignal("Value"):Connect(function()
				if TimeUntilAvailable.Value == 20 then
					Touched.Value = false
				end
			end)
		end
	
		Player.CharacterAdded:Connect(function()
			CharacterHasReset.Value = true
		end)
	end
end

script.Parent.Touched:Connect(onTouched)

Script 3, Creating the CharacterHasReset Value. (Insert Script into StarterCharacterScripts)

local CharacterHasReset = Instance.new("BoolValue", script.Parent)
CharacterHasReset.Value = true
CharacterHasReset.Name = "CharacterHasReset"

Also there may be a simpler way of doing this but I over complicate things when I'm tired. Anyway, Hopefully it all works for you. It should as I just tested and it worked perfectly for me, but if it didn't work for some reason, post a comment and I'll try and help you fix it.

Basically what this will do is it will check if the Parts touchable by checking the Bool Value we added to the Part (isTouched) and it also check if the Timer is equal to 0. If the Timer is equal to 0 and the part is equal to false, using false because we first need to check if it's the value is equal to false then set it to true right after so it wont run again. And we're also checking if the CharacterHasReset is equal to true, if it's equal to true then that means the Character has reset, then they can collect the Coins, if it's not equal to true and it's equal to false then they cannot collect the coins 'til their Character gets respawned, which they'll need to die for that to happen.

8 comments

0

all of them in normal scripts or local ones 2?

0

also i placed the timer as a lazy fix so players can't do it over and over again that is why i want them to be able to get credits after the OOF

0

also thank you for help but i literally get errors at every line

0

They are all in regular scripts(Server Scripts). Also, what are the errors?

0

The errors have to be because the variable locations are wrong, etc. Because like I said, I've already tested it and it all worked. Make sure all variable locations are right.

0

The Number Value which is the Timer should be stored inside the Part that you are touching, same with the first two scripts, the third script is stored inside of StarterCharacterScripts. The Bool Value "isTouched" needs to be inside the Part that the players will be touching for the Coins also. Make sure they're all right, otherwise you will get errors if the variable locations are wrong.

0

If you need to change the variable locations because it cannot find these, then you can do so if needed.

0

On the second script you've put an event inside an event, which will cause the property changed signal to be started over and over again. I would not recommend this. You can simply make a timer with a debounce and waiting 20 seconds, all in the same script.

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