DataStore doesn't save data

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)
1 Like

Clicks have to be changed server side
Use a remoteevent

Also, use a boolean as a cooldown for the button1down function

1 Like

It works now :slight_smile:
But why does it not work when the clicks are in a localscript?

You have to change the value of an intvalue from the server

server does not see any changes was made on client, there’s why.

You desire that client side being unable to change what server knows, otherwise. A client could use an exploit tool to change any value on their side, and server values would be changed too, saved, and game hacked…

2 Sides, client which means the values that only exist in the device that the player is using, and Server, the values that the roblox server of your game is holding.
If you want the client to ask a change on server values, you use remotes, and verify they are allowed to do that change

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.