✓ Accepted answer
Just because your function is connected to an event (like a ClickDetector's MouseClick event) doesn't mean that the code will run any differently. This works the same as any other code that uses IncrementAsync, although you might want to use variables outside of the connected function to avoid sending a datastore request on every single click.
An example of using variables outside of the connected function so that you don't need to send constant requests would be:
local clickDetector = workspace.something.clicky
local dsService = game:GetService("DataStoreService")
local timesClickedDS = dsService:GetDataStore("TimesClicked")
local playerService = game:GetService("Players")
local timesClickedTable = {}
clickDetector.MouseClick:Connect(function(player)
if timesClickedTable[player.UserId] then
timesClickedTable[player.UserId] = timesClickedTable[player.UserId] + 1
else
timesClickedTable[player.UserId] = 1
end
end)
playerService.PlayerRemoving:Connect(function(player)
local pUserId = player.UserId
if timesClickedTable[pUserId] then
timesClickedDS:IncrementAsync(pUserId, timesClickedTable[pUserId]
end
end)
playerService.PlayerAdded:Connect(function(player)
while true do
wait(300)
if timesClickedTable[player.UserId] then
timesClickedDS:IncrementAsync(player.UserId, timesClickedTable[player.UserId])
end
end
end)
In this particular example, someone would be able to autoclick to increment the counter a lot more than (probably) intended, as there's no debounce or anything that will change once they've clicked it once.
As I'm pretty sure you've figured out (judging by your question), you can find a lot of information on the wiki page for datastores.
Edit: Since you wanted an answer where the value is available between scripts, you can adapt the original code to use IntValues instead of script-specific variables. This wouldn't require much change, but I'll repost the whole thing edited as the changes are spread throughout the script. Changed lines will be marked with "--***--" above them.
local clickDetector = workspace.something.clicky
local dsService = game:GetService("DataStoreService")
local timesClickedDS = dsService:GetDataStore("TimesClicked")
local playerService = game:GetService("Players")
local timesClickedTable = {}
clickDetector.MouseClick:Connect(function(player)
if timesClickedTable[player.UserId] then
timesClickedTable[player.UserId].Value = timesClickedTable[player.UserId].Value + 1
else
local intValueSetup = Instance.new("IntValue")
intValueSetup.Name = "timesClicked"
intValueSetup.Value = 1
timesClickedTable[player.UserId] = intValueSetup
end
end)
playerService.PlayerRemoving:Connect(function(player)
local pUserId = player.UserId
if timesClickedTable[pUserId] then
timesClickedDS:IncrementAsync(pUserId, timesClickedTable[pUserId].Value)
end
end)
playerService.PlayerAdded:Connect(function(player)
while true do
wait(300)
if timesClickedTable[player.UserId] then
timesClickedDS:IncrementAsync(player.UserId, timesClickedTable[player.UserId].Value)
end
end
end)
As you can see, it is largely the same aside from adding .Value to check the IntValue's value and extra setup for the IntValue since it's an object not just a number. If there's any confusion, I'll likely edit the post later.
10 comments
Great stuff, this really helped me out. One more quick question though - if I wanted to request this data in another script, like a leaderboard, how would I get it? Would I make a reference to TimesClicked, or maybe the UserId?
Instead of having a table of player userid to value, you could have a table of player userid to an IntValue object (parented to the player) so that you can access it from other scripts
Sounds good, what line of code could I use that would instruct the table contents to be deposited into the IntValue? Thanks again.
it'd be multiple lines so I'll edit original answer to have an IntValue solution
After some testing, its seems that the ModuleScript isn't picking up on when clickDetector is clicked. I doubled checked, the adress of the detector and the adress in the script, and they match. Could it be because I put the ModScript in the wrong place (ServerStorage)?
ModuleScripts don't run on their own, they're like shared code that can be accessed between scripts when required
Does that mean the script needs to be active and running before it'll be able to register clicks from the clickDetector?
this script would work fine as a script, try checking the values from another normal script rather than a module script
Checked it out in a normal script, it doesn't seem to produce any errors. Still though, I'm not seeing an IntValue object being created no matter where the script is/what type of script it is. Any suggestions?
It's created once they've clicked at least once under the player, are you checking before or after you've clicked?