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

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
4 years ago
Edited 4 years ago

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.

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 HeroFigo 81 — 4y
0
Is that part of the script working? sheepposu 561 — 4y
0
ye but i don't know lua so much, i can fix things but can't make a fully working script without tutorials HeroFigo 81 — 4y
0
So are you still having any problems? and if so then what are the problems you're having? sheepposu 561 — 4y
View all comments (2 more)
0
i don't know how to make a boolean HeroFigo 81 — 4y
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" sheepposu 561 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
all of them in normal scripts or local ones 2? HeroFigo 81 — 4y
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 HeroFigo 81 — 4y
0
also thank you for help but i literally get errors at every line HeroFigo 81 — 4y
0
They are all in regular scripts(Server Scripts). Also, what are the errors? xInfinityBear 1777 — 4y
View all comments (4 more)
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. xInfinityBear 1777 — 4y
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. xInfinityBear 1777 — 4y
0
If you need to change the variable locations because it cannot find these, then you can do so if needed. xInfinityBear 1777 — 4y
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. sheepposu 561 — 4y

Answer this question