I made this code which gives cash to a player when they click on a part they get cash. How would I add the condition that if a player has already clicked on the same part then they can't press it again but they can press on other parts which have the same clickdetectorevent.
My ClickdetectorEvent Code:
workspace.MyClickDetectorEvent.OnServerEvent:connect(function(plr) plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + (1000) end)
Each part has a clickdetector and has the fire event code in localscript in startergui
How would I make it so that if a player has already clicked on the part once then they cant get cash if they press again but they can click on a different part with the same event and still get cash.
The code i made so that if a player has already clicked on the part they wont get cash again however this caused player s unable to click on another part which has the same click detector event but in my game players should be able to.
local connection connection = workspace.MyClickDetectorEvent.OnServerEvent:Connect(function(plr) plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + 5 connection:Disconnect() end)
Hi aspiringstar346,
local plrs_already_clicked = {} -- The table that will have the players that have clicked the part. script.Parent.MouseClick:Connect(function(plr) -- Anonymous function with the parameter of the player that clicked it. if not plrs_already_clicked[plr] then -- Checks if the player exists. The reason I am doing [] is because I am going to put it in the table like that, as a dictionary. You may see below what I mean. print("This player hasn't clicked it yet."); --[[ Prints this if the player hasn't clicked it yet. plrs_already_clicked[plr] = true; -- Here, it saves the player to the table so if the player clicks it again, it will have it in the table and will not run this if statement. The index for the table is the player object, and the value is true. So, I index the table with player objects, that's what the [' '] is used for. --]] else -- Runs if the player has already clicked it. print("This player has clicked it already.") -- Prints this since the player has already clicked it before. end -- end for if statement end) -- end for anonymous function.
Thanks,
Best regards,
~~ KingLoneCat
workspace.MyClickDetectorEvent.OnServerEvent:connect(function(plr) plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + (1000) script:Destroy end)