Datastore Won't Save Value [HELP]

I just used this datastore right now, but when i use this code it just add values to leaderstats and it prints data has been saved but when i rejoin its still 0 value, not saved.

The code in StarterGui as a LocalScript:

local ScreenGui = Instance.new("ScreenGui")
ScreenGui.Enabled = true
ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")


local TextButton = Instance.new("TextButton")
TextButton.Size = UDim2.new(0, 200, 0, 50)
TextButton.Position = UDim2.new(0.5, -100, 0.5, -25)
TextButton.Text = "Strength +1"
TextButton.Parent = ScreenGui

TextButton.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
end)

Can you give me the datastore code???

The problem is it’s a LocalScript. Anything changed on the client will only change for that client. You need to change the value on the server for it to save.

1 Like
-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Strength.Value; -- First value from the table
		player.leaderstats.Wins.Value -- Second value from the table
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game

	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

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

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

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success and data then -- If there were no errors and player loaded the data

		Strength.Value = data[1] -- Set the money to the first value of the table (data)
		Wins.Value = data[2] -- Set the coins to the second value of the table (data)

	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

But it should be local script, if the player click the gui he should earn points.

please put the script in serverscriptservice

Datastore already in serverscript service? Should i put the screengui script to serverscript?

Update the value on the server. The client’s GUI should mimic the value - no data needed for saving should be edited on the client.

You can use a RemoteEvent or a RemoteFunction to do this.

2 Likes

Thank you, i will try this one right now.

1 Like

The same thing happened to me but I solved it

you can create 1 script for each value, for me it worked

1 Like

Ever heard of client-server model roblox uses? Client-Server Runtime | Documentation - Roblox Creator Hub

1 Like

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