Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this datastore doesn't load my data correctly?

I think the title explains it all. Here is my code:

local dss = game:GetService("DataStoreService")
local datastore = dss:GetDataStore("PurchaseCarDataStore")
local intvalue = game.ReplicatedStorage.Purchasedbmw
	game.Players.PlayerAdded:Connect(function(player)
	local playergui = player:WaitForChild("PlayerGui")
	local purchasegui = playergui:WaitForChild("PurchaseGUI")
	local frame = purchasegui:WaitForChild("Frame")
	local bmw = frame:WaitForChild("bmw")
	local textbutton = bmw:WaitForChild("TextButton")
	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats"
	local cash = Instance.new("IntValue",folder)
	cash.Name = "Cash"
		local savedData = nil
		local ID = intvalue.Name.."-"..player.UserId
		print(ID)
		pcall(function()
			savedData = datastore:GetAsync(ID)
		end)
		
		if savedData ~= nil then
			intvalue.Value = savedData
			print("Data loaded! - "..intvalue.Value)
		else
			intvalue.Value = 0
			print("New player to the game, value is false - "..intvalue.Value)
		end
		if intvalue.Value == 1 then
			textbutton.Text = "Spawn"
		end
	end)
	game.Players.PlayerRemoving:Connect(function(player)
		local ID = intvalue.Name.."-"..player.UserId
		datastore:SetAsync(ID,intvalue.Value)
	end)
	game:BindToClose(function()
		for i,player in pairs(game.Players:GetPlayers()) do
			if player then
				player:Kick()
			end
		end
	end)

The problem is, when I first join the game everything works fine, but when I rejoin it says that data was loaded, however, before leaving the game and rejoining I changed the value to 1, and when I rejoin it says "Data loaded - 0" (it's supposed to say 1 instead of the 0). Also, the text doesn't change to "Spawn". Any fixes?

2 comments

0

How did you change the value? Because if you know the difference between client and server, you'll know that client changes are not seen by the server, make sure the value is changed on the server and not the client

0

I changed it using a local script, so yeah, I changed it using client, and not with the server. I'll try changing it with the server.

1 answer

As rower86 said, if i change the value with the client the server doesn't see it. I'm using now a server script and it's working totally fine! Thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.