Help with datastore

for some reason, when I tested my game in public with 2 accounts, on each screen it says the other player has 0 but it says that the current account has an amount (like 50 or whatever they were given). Here is my datastore script which is not working.

--serverScriptService server side script
local DSS = game:GetService("DataStoreService")
local mainDS = DSS:GetDataStore("CoinsData")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:FindFirstChild("Events")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue", leaderstats)
	coins.Name = "Coins"
	
	local data = mainDS:GetAsync(player.UserId)
	
	if data then
		if data[1] then 
			coins.Value = data[1]
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local coins = leaderstats:WaitForChild("Coins")
	
	mainDS:SetAsync(player.UserId, {coins.Value})
end)

here is the scripts that gives money

--StarterCharacterScript, local script
local chest = game.Workspace.Chest
local Open = chest.ChestOpen:GetChildren()
local Closed = chest.ChestClosed:GetChildren()
local prompt = game.Workspace.Chest.Button.ProximityPrompt


local function ClosedChestTrans(Transparency)
	for i,v in pairs(Closed) do
		v.Transparency = Transparency
	end
end


local function OpenChestTrans(Transparency)
	for i,v in pairs(Open) do
		v.Transparency = Transparency
	end
end

ClosedChestTrans(0)
OpenChestTrans(1)


prompt.Triggered:Connect(function(plr)
	if plr == game.Players.LocalPlayer then
		ClosedChestTrans(1)
		OpenChestTrans(0)
		game.Workspace.Chest.Button.ProximityPrompt.Enabled = false
		game.Players.LocalPlayer.leaderstats.Coins.Value += 500
		game.Players.LocalPlayer.PlayerGui.Chest.Enabled = true
	end
end)

second coin giving script

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local toolName = "Wood"
game.Workspace.Guide:WaitForChild("Head")
game.Workspace.Guide.Head:WaitForChild("ProximityPrompt")
local tool
local deb = 0

while wait(0) do
	-- Check if the tool is equipped by the player
	 tool = character:FindFirstChild(toolName)

	-- Enable proximity prompt if the tool exists and is equipped
	if tool then
		deb = 0
		game.workspace.Guide.Head.ProximityPrompt.Enabled = true

		-- Move the tool to game.ReplicatedStorage.Supplies when the prompt is triggered
		game.workspace.Guide.Head.ProximityPrompt.Triggered:Connect(function(plr)
			
			if deb == 0 and plr == player then
				tool.Parent = game.ReplicatedStorage.Supplies
				player.leaderstats.Coins.Value += 50
				deb = 1
			end
		end)
	else
		game.workspace.Guide.Head.ProximityPrompt.Enabled = false
	end
end

ALL HELP APPRECIATED THANKS!

1 Like

It seems you’re encountering synchronization issues with player data in your Roblox game. Here are some key points to address:

DataStore Script:

  • Ensure that you use player.UserId as the key for your DataStore to uniquely identify players.
  • Double-check that your DataStore is properly set up with the correct name in the Roblox DataStore dashboard.

Giving Coins Script:

  • Verify that the leaderstats folder is consistently created for each player when they join the game.
  • Make sure you’re not unintentionally resetting players’ coin counts to 0 elsewhere in your code.

Second Coin-Giving Script:

  • Confirm that the script checks for equipped tools and adds coins to the player’s leaderstats correctly.

To address the issue where players see each other as having 0 coins, consider adding debug messages in your code to trace data flow and identify discrepancies. This will help pinpoint and resolve the synchronization problem.

Roblox debugging can be challenging, but with thorough testing and error checking, you should be able to find and fix the issue.

1 Like

Idk why but this sounds like something chatgpt would say

2 Likes

You are on chatgpt to much, but anywho, it’s not.

1 Like

You’re changing the coins stat on the player’s client not the server so it won’t replicate

1 Like
player.leaderstats.Coins.Value += 50

You are increasing this value on the client. This wont replicate unless you increase it on the server. You need to send a remote to the DataStore script and increase the value there.

1 Like

You don’t have an remoteevents specified to call the action from the localscript to the server so it will never store in the database.

1 Like

Doing it on the server instead of a remote would be better to prevent exploits.

1 Like

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