Can't update leaderstat's values

I’ve found DataBase script in DevForum like that:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")

local function saveData(player) -- Function that saves the data
	local tableToSave = {
		player.leaderstats.Coins.Value -- Sec value fro..
	}
	
	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
		print("Data has been saved successfully!")
	else
		print("ERR: Data hasn't been saved!")
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(function(player)

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

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

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId)

	end)

	if success and data then

		Coins.Value = data[1]

	else
		print("The player has no data!") -- The default will be set to 0	
		
		Coins.Value = 0
		
	end

end)

game.Players.PlayerRemoving:Connect(function(player) -- Gets the player that left.
	saveData(player) -- Save the data when player left.
end)

game:BindToClose(function() -- When the server shuts down (When last player left from server, saves before shutdowning server.)
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		saveData(player) -- save the data
	end
end)

I am trying to change coins when someone clicked to imagebutton. So i made this in Starter Gui> Local Script.

local Players = game:GetService("Players")

local right = false
local player = game:GetService("Players").LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local coins = leaderstats.Coins.Value

for i, Button in pairs(script.Parent.AnswerGui.Frame:GetChildren()) do

	if not Button:IsA("ImageButton") then continue end

	Button.MouseButton1Click:Connect(function()
		local answersimg = Button.Image
		if string.find(answersimg:lower(), script.Parent.Answer.Value) then
			right = true
			script.Parent.Question.Value = "Correct!"
			coins = coins + 1
		else
			right = false
			script.Parent.Question.Value = "Incorrect, the correct answer is "..script.Parent.AnswerInText.Value
		end
		
		game.ReplicatedStorage.SendQuestion:FireServer(right)
		
	end)
end

When i type print(“coins.value”) it works, it addes +1 coins when clicked but it won’t change in leaderstats. I know there should be set/update attribute or something but i am so bad at scriptting. Please someone help. Thanks!

If you change a value using a LocalScript, that value will only change for the player only. To make the changed value visible for other players or the server, you’ll need to either change the value inside of a server Script or use a RemoteEvent to send a signal to a server Script from a LocalScript

Thank you, I understand now! But could you please help me to send signal and recieve from DataBase? I am so bad at scripting…

1 Like

The LocalScript will need to be changed like so:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local right = false

for i, Button in pairs(script.Parent.AnswerGui.Frame:GetChildren()) do

	if not Button:IsA("ImageButton") then continue end

	Button.MouseButton1Click:Connect(function()
		local answersimg = Button.Image
		if string.find(answersimg:lower(), script.Parent.Answer.Value) then
			right = true
			script.Parent.Question.Value = "Correct!"

			ReplicatedStorage.UpdateLeaderstats:FireServer()
		else
			right = false
			script.Parent.Question.Value = "Incorrect, the correct answer is "..script.Parent.AnswerInText.Value
		end

		ReplicatedStorage.SendQuestion:FireServer(right)

	end)
end

And for the server Script:

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local dataStore = DataStoreService:GetDataStore("MyDataStore")

local updateLeaderstats = ReplicatedStorage.UpdateLeaderstats

local function saveData(player) -- Function that saves the data
	local tableToSave = {
		player.leaderstats.Coins.Value -- Sec value fro..
	}

	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
		print("Data has been saved successfully!")
	else
		print("ERR: Data hasn't been saved!")
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(function(player)

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

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

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId)

	end)

	if success and data then

		Coins.Value = data[1]

	else
		print("The player has no data!") -- The default will be set to 0	

		Coins.Value = 0

	end

end)

updateLeaderstats.OnServerEvent:Connect(function(player)
	player.leaderstats.Coins.Value += 1
end)

game.Players.PlayerRemoving:Connect(function(player) -- Gets the player that left.
	saveData(player) -- Save the data when player left.
end)

game:BindToClose(function() -- When the server shuts down (When last player left from server, saves before shutdowning server.)
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		saveData(player) -- save the data
	end
end)

You’ll also need to create a new RemoteEvent named “UpdateLeaderstats” inside of ReplicatedStorage for the scripts to work

1 Like

You are best, it works perfect. Thanks.

1 Like

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