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

How would I script for a player to only be able to click on a part once in every server?

Asked by 6 years ago

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)
0
That, my friend. I don't know :P Nep_Ryker 131 — 6y
0
oh ok aspiringstar346 8 — 6y
0
u need to hav brain soemthing u dont hav Sqnister -12 — 6y
0
probably need a datastore or something idk ATestAccount420 31 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Hi aspiringstar346,

First off, lemme just apologize for those people in the comments of your question, they are rude for not much of a reason what-so-ever. Anyways, you can easily accomplish the task you need with a dictionary/table(whichever way you look at it). Just add each player's name in the table like you would access a dictionary and set it equal to true for that player. Then just check for that player every time a click is detected. This will be much easier if I show you what I mean in a script rather than just be theoretical.

Here:

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.

Anyways, I hope I helped. You can check out how dictionaries work here.

Have a nice day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
Would I put the script in the part with the click detector aspiringstar346 8 — 6y
0
You would put the script under the click detector. KingLoneCat 2642 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
workspace.MyClickDetectorEvent.OnServerEvent:connect(function(plr)

    plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + (1000)
   script:Destroy

end)


0
How would I say that the script would only destroy for the player that clicked it aspiringstar346 8 — 6y
0
how would i make the part that is clicked destroyed aspiringstar346 8 — 6y

Answer this question