Problem: My Server Script doesnt save anything when leaving.
I think it has something to do that the player already left the game and so the game cant find Clicks anymore.
– Server Script
local DataStoreService = game:GetService("DataStoreService")
local PlayerClicks = DataStoreService:GetDataStore("PlayerClicks")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local success, currentclicks = pcall(function()
return PlayerClicks:GetAsync(player.UserId)
end)
if success then
Clicks.Value = currentclicks
else
Clicks.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMsg = pcall(function()
PlayerClicks:SetAsync(player.UserId, player.leaderstats.Clicks.Value)
end)
if success then
print("successfully saved")
else
warn(errorMsg)
end
end
– local script adds + 1 to Clicks
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.Button1Down:Connect(function()
local Clicks = Player:WaitForChild("leaderstats").Clicks
Clicks.Value = Clicks.Value + 1
task.wait(0.1)
end)