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

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

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.

3 comments

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.

0

Sorry, I am a begginar, if not the script can you please tell me what i can do

0

Look at the anwer

2 answers

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.

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!

1 comment

0

Also, you'd run this function when the player completes the task.

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