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

How to code if a player clicks a part again the function doesn't happen?

I made this code that if a player clicks on a part they get five cash, how do I add that if the player has already clicked on the part once the second time they press it it doesn't award the player with money?

My Clickdetector event code:

workspace.MyClickDetectorEvent.OnServerEvent:connect(function(plr)

    plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + (5)


end)

Thank You and help is appreciated

3 answers

You would use the disconnect function for RBXScriptSignals, otherwise known as events.

local connection
connection = workspace.MyClickDetectorEvent.OnServerEvent:Connect(function(plr)
	plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + 5
	connection:Disconnect()
end)
workspace.MyClickDetectorEvent.OnServerEvent:connect(function(plr)

local found = plr.PlayerGui:FindFirstChild("NoAddCash")
if not found then
	local val = Instance.new("BoolValue")
	val.Name = "NoAddCash"
	val.Parent = plr.PlayerGui
	plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + (5)
end  
 
end)

this should work. not tested

If you're talking about all players you could use this:

local enabled = true --Set enabled to true, so the script is usable

workspace.MyClickDetectorEvent.OnServerEvent:connect(function(plr)
	if enabled == true then --Checks if the bool is true
		plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + (5)
		enabled = false --Sets the bool to false, making it unusable
	end
end)

Have fun scripting!

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