Datastore not working

So i have this code that i found with data store i am trying to fix it for like a day now using different codes and everything and i really dont know how to do it so here`s the code

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDatastore = DataStoreService:GetDataStore("SaveDatastore")

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local clicks = Instance.new("IntValue")
	clicks.Name = "clicks"
	clicks.Parent = leaderstats
	
	local upgrades = Instance.new("IntValue")
	upgrades.Name = "Upgrades"
	upgrades.Parent = leaderstats
	upgrades.Value = 1
	
	local rebirths = Instance.new('IntValue')
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	local Data
	local Success,result = pcall(function()
		return SaveDatastore:GetAsync(player.UserId)
	end)
	if Success then
		if result then
			print("Sucessfully saved data")
			print(result.clicks)
			clicks.Value = result.clicks
			upgrades.Value = result.upgrades
			rebirths.Value = result.rebirths
		else
			print("Player is new.")
		end
	else
		warn("Error occured \n \n"..result)
	end
end
Players.PlayerAdded:Connect(onPlayerJoin)

Players.PlayerRemoving:Connect(function(player)
	local Data = {
		clicks = player.leaderstats.clicks.Value,
		upgrades = player.leaderstats.Upgrades.Value,
		rebirths = player.leaderstats.Rebirths.Value
	}
	local Success,result = pcall(function()
		SaveDatastore:SetAsync(player.UserId, Data)
	end)
	if Success then
		print("Succesfully saved data")
		print(Data)
	else
		warn("Error occured \n \n"..result)
	end
end)
game:BindToClose(function()
	task.wait(1)
end)

and the problem is that when i set the value in

if result then
			print("Sucessfully saved data")
			print(result.clicks)
			clicks.Value = result.clicks
			upgrades.Value = result.upgrades
			rebirths.Value = result.rebirths
		else
			print("Player is new.")
		end

to something else like:

if result then
			print("Sucessfully saved data")
			print(result.clicks)
			clicks.Value = 15
			upgrades.Value = result.upgrades
			rebirths.Value = result.rebirths
		else
			print("Player is new.")
		end

then it will remeber that clicks.Value is 15 but when i try do acquire clicks in game and leave, it will not save for some reason and i dont know why pls help

Make sure this is enabled.

Datastores work like this:

  1. Create a database
  2. Save key-value pairs to the database
  3. Get key-value pairs from the database
local DataStoreService = game:GetService('DataStoreService') 
local db = DataStoreService:GetDataStore('test') 
db:SetAsync('123','hi!') 

This created the database and added a [‘123’]=‘hi’; to it.

print(db:GetAsync('123')) -- hi! Instance
-- The Instance is a DataStoreKeyInfo object, ignore it.

The following code uses the data retrieved from the DataStore to set the values. You should not touch it.

If you want to change the value, for example whenever a player clicks, you need to count clicks and then when the player leaves send the amount of clicks to the server.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDatastore = DataStoreService:GetDataStore("SaveDatastore")

local ClickRemoteEvent = game.ReplicatedStorage:FindFirstChild('Click') or Instance.new('UnreliableRemoteEvent',game.ReplicatedStorage) ClickRemoteEvent.Name = 'Click'

ClickRemoteEvent.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild('leaderstats')
	local clicks = leaderstats:FindFirstChild('clicks')
	clicks.Value += 1
end)

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local clicks = Instance.new("IntValue")
	clicks.Name = "clicks"
	clicks.Parent = leaderstats

	local upgrades = Instance.new("IntValue")
	upgrades.Name = "Upgrades"
	upgrades.Parent = leaderstats
	upgrades.Value = 1

	local rebirths = Instance.new('IntValue')
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats

	local Data
	local Success,result = pcall(function()
		return SaveDatastore:GetAsync(player.UserId)
	end)
	if Success then
		if result then
			print("Sucessfully saved data")
			print(result.clicks)
			clicks.Value = result.clicks
			upgrades.Value = result.upgrades
			rebirths.Value = result.rebirths
		else
			print("Player is new.")
		end
	else
		warn("Error occured \n \n"..result)
	end
end
Players.PlayerAdded:Connect(onPlayerJoin)

Players.PlayerRemoving:Connect(function(player)
	local Data = {
		clicks = player.leaderstats.clicks.Value,
		upgrades = player.leaderstats.Upgrades.Value,
		rebirths = player.leaderstats.Rebirths.Value
	}
	local Success,result = pcall(function()
		SaveDatastore:SetAsync(player.UserId, Data)
	end)
	if Success then
		print("Succesfully saved data")
		print(Data)
	else
		warn("Error occured \n \n"..result)
	end
end)
game:BindToClose(function()
	task.wait(1)
end)



localscript

local UIS = game:GetService('UserInputService')

local ClickRemoteEvent = game.ReplicatedStorage:WaitForChild('Click')

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		ClickRemoteEvent:FireServer()		
	end
end)

1 Like

you used replicated storage and i used just a local script and thats why it wasnt working so thanks to you i realized that, thanks lova ya <3

Local scripts can not access DataStores. Only server scripts can.

2 Likes

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