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

What to do if you want a function to only happen once and not keep repeating?

Asked by 4 years ago

If I want a player to get a reward after completing something and I only want that to happen once. I mean even if he completes it one more time he won't get the reward. Explain and tell the script.

0
First off, this isn't a script request site. Second, we don't have any scripts to work with here, making it harder to solve your question. Dan_PanMan 227 — 4y
0
Sorry, I am a begginar, if not the script can you please tell me what i can do iwasinevitib1e 3 — 4y
0
Look at the anwer alexfinger21 341 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Hello! I will give you an abstract Idea to how to do it, I am going to make a touch function that will only work once:

local debounce = true
script.Parent.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) and debounce then
        debounce = false
        wait(1)
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        print("Hit by plr named "..plr.Name)
    end
end)

As you can see, I used the debounce variable, which lets you only step on the brick once. Hope it helps! Please accept if helps.

Ad
Log in to vote
0
Answered by 4 years ago

I think I understand your question. Here is an example below:

local canReward = 1 --You'd start this at 1 for reasons later.

function reward()
    if canReward == 1 then
        --Run the rewarding script in here

        canReward = 0
        --Set canReward to 0 so the function cannot reward again
    end
    if canReward == 0 then
        print("Player has already been rewarded!")
    end
end

--Basically what this script does is it has an overall reward variable. If that variable is 1, then the player gets rewarded and the variable gets set to 0, which means that it cannot reward the next time the player does the task.

Hope this helped, and happy scripting!

0
Also, you'd run this function when the player completes the task. blarp_blurp645 63 — 4y

Answer this question