local DSS =game:GetService("DataStoreService")
local ClickDataStore = DSS:GetDataStore("ClickDataStore")
game.Players.PlayerAdded:Connect(function(player)
local Stat = Instance.new("Folder")
Stat.Name = "Leaderstats"..player.Name
Stat.Parent = game.Players
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = Stat
local data
local success, errormessage = pcall(function()
data = ClickDataStore:GetAsync(player.userId.."Clicks")
end)
if success then
Clicks.Value = data
print("Player Data successfully loaded!")
print("Last saved Click data:", data)
else
print("There was an error when loading data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
print("Most recent Click data save:", player.leaderstats.Clicks.Value)
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks",game.Players["LeaderStats"..player.Name].Clicks.Value)
end)
if success then
print("Player Data successfully saved!")
game.Players["LeaderStats"..player.Name]:Destroy()
else
print("There was an error when saving data")
warn(errormessage)
end
end)
If a player joins for the first time, data will be nil, so checking if “success” is true won’t be enough. What you need to do is to give default data, if no previous data exists. You can simply do this by changing the line with “GetAsync” to the following:
data = ClickDataStore:GetAsync(player.userId.."Clicks") or 0 -- If no data, give starter data
Ok, thanks. I have changed it. But when I leave the game this is what is show in the output. It seems it cant save but I don’t know why.
15:18:44.111 Physics-in of unidentified 1_968803 - Studio
15:18:44.210 Player Data successfully loaded! - Server - ClickDataStore:18
15:18:44.210 Last saved Click data: 0 - Server - ClickDataStore:19
15:18:49.805 New tag editor version coming online; unloading the old version - Studio
15:18:49.944 Most recent Click data save: 0 - Server - ClickDataStore:27
15:18:49.945 There was an error when saving data - Server - ClickDataStore:36
15:18:49.945 LeaderStatsAapocalypse_Gaming is not a valid member of Players "Players" - Server - ClickDataStore:37
15:18:49.956 New tag editor version coming online; unloading the old version - Studio
15:18:49.943 Disconnect from ::ffff:127.0.0.1|50572 - Studio
The saving system is not very well done, and you should anticipate that the player will be completely gone as soon as possible. Here is an optimized version:
game.Players.PlayerRemoving:Connect(function(player)
local PlayerClicks = player.leaderstats.Clicks.Value
print("Most recent Click data save:", PlayerClicks )
local success, errormessage = pcall(function()
ClickDataStore:SetAsync(player.UserId.."Clicks", PlayerClicks)
end)
if success then
print("Player Data successfully saved!")
else
print("There was an error when saving data")
warn(errormessage)
end
end)
You’re changing the value in a Localscript. Just take this script and put it in a ServerScript in the same location as the Localscript:
script.Parent.MouseButton1Click:Connect(function()
local player = script:FindFirstAncestorOfClass("Player")
player["LeaderStats"..player.Name].Clicks.Value += 1
end)
-- make a remoteevent in replicated storage called ClickRemote
local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
if debounce == false then
player.leaderstats.Clicks.value += 1
debounce = true
end
if debounce == true then
wait(2)
end
end)
-- local script
script.Parent.MouseButton1Click:Connect(function()
clickRemote:FireServer()
end)
-- or
--Server Script inside button
local debounce = false
game:GetService("Players").PlayerAdded:Connect(function(player)
script.Parent.MouseButton1Down:Connect(function()
if debounce == false then
player.leaderstats.Clicks.value += 1
debounce = true
end
if debounce == true then
wait(2)
end
end)
end)
local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
if debounce == false then
player.leaderstats.Clicks.value += 1
debounce = true
end
if debounce == true then
wait(2)
end
end)
-- local script
script.Parent.MouseButton1Click:Connect(function()
clickRemote:FireServer()
end)
-- or
--Server Script inside button
local debounce = false
There’s absolutely no checking for existing players before PlayerAdded can be fired, most of these replies need to stop.
You need to declare a function that handles players, not just branching directly off the event connection.
function PlayerHandle(Player)
--do stuff here
end
for i,v in next, game.Players:GetPlayers() do
PlayerHandle(v)
end
game.Players.PlayerAdded:Connect(PlayerHandle)
local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
if debounce == false then
player.leaderstats.Clicks.value += 1
debounce = true
end
if debounce == true then
wait(2)
end
end)
local clickRemote = game:GetService("ReplicatedStorage"):WaitForChild("ClickRemote")
local debounce = false
-- server
clickRemote.OnServerEvent:Connect(function(player)
if debounce == false then
player.leaderstats.Clicks.value += 1
debounce = true
end
if debounce == true then
wait(2)
end
end)
I don’t get how this is confusing the one that has the event for the button goes inside a localscript inside of the button which increases the players clicks.
The one with the debounce goes inside of a regular script/serverscript in serverscriptservice.
also that doesn’t work because the path is created in the datastore. eg it creates a folder called LeaderstatsAapocalypse_Gaming im my case and then in that makes the Clicks value.
game.Players.PlayerAdded:Connect(function(player)
local Stat = Instance.new("Folder", player)
Stat.Name = "Leaderstats"..player.Name
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = Stat