DataStore script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to successfully create a DataStore script

  2. What is the issue?
    Well… Im trying to learn how to script but had a little problem:
    While watching a tutorial about DataStore i wrote the entire script and tested it,but for some reason when i changed the value of the “Coins” and then stopped and started the game again it didn’t save!

  3. What solutions have you tried so far?
    I have tried solving it by myself with the output, but it doesn’t tell me any errors and i also can’t find any!

Well,here is the script, i really don’t understand what im doing wrong here.

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

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, errorMessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success then
		coins.Value = data
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		local data = myDataStore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
	end)

	if success then
		print("Data Saved")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetChildren()) do
		player:Kick("Server Closed")
	end

	wait(2)
end)

You probably changed the coins through the explorer. Changing it through the explorer without first changing to the server view will only change it locally, meaning it won’t save. You can change it using this in console:

game.Players.Cristinoboga.leaderstats.Coins.Value = 1000
2 Likes

Thank you so much man, this really helped me.
I understood how to do it :slight_smile: