Saving money (datastore)

image
why yes it is

Alright I am sorry. I might have been in the wrong.

1 Like

Its fine, after all, we are all trying to find solutions

1 Like

Hello I hope this helps:

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = Leaderstats

	local MoneyData

	local Success, ErrorMessage = pcall(function()
		MoneyData = DataStore:GetAsync(Player.UserId.."-MoneyData")
	end)

	if MoneyData ~= nil then
		Money.Value = MoneyData
	end

end)

game.Players.PlayerRemoving:Connect(function(Player)
	pcall(function()
		DataStore:SetAsync(Player.UserId.."-MoneyData", Player.leaderstats.Money.Value)
	end)
end)

Sorry, but this doesnt work either :sad: No error message

But, it wouldn’t detect any changes, so it would not change the money value on the datastore. There should be something like IntValue.Changed:Connect -- etc...

Hey, are you changing the values on server and not client? this might be the issue.

1 Like

@x8yv Oh yeah, extra detail

Money:GetPropertyChangedSignal("Value"):Connect(function()
 Storage[p.UserId]["Cash"] = Money.Value -- Will Update the Table
end)

else -- If Failed to Access DataStores
-- maybe retry or kick the player?
end

Money.Value = Storage[p.UserId]["Cash"] -- Value will equal to Table data

Money:GetPropertyChangedSignal("Value"):Connect(function()
 Storage[p.UserId]["Cash"] = Money.Value -- Will Update the Table
end)
end)

local Data = game:GetService("DataStoreService"):GetDataStore("THEDATA")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("NumberValue",leaderstats)
	money.Name = "money"
	money.Value = 0
	
	local DataLoad = Data:GetAsync(tostring(player.UserId))
	
	if DataLoad then
		money.Value = DataLoad[1]
	elseif not DataLoad then
		error("There was an error while loading the leaderstats.")
	end
	
	task.wait(0.4)
	
	money.Value += 100
end)

game.Players.PlayerRemoving:Connect(function(player)
	Data:GetAsync(tostring(player.UserId),{
		player.leaderstats.money.Value
	})
end)

Yes the Error was made by me, and it kinda-source-errors.

Ah, I get it, it’s nil.

Add a remote event, and name it for example “testRemote” into ReplicatedStorage.
DELETE THIS AFTER YOU ARE DONE TESTING OTHERWISE EXPLOITERS WILL ABUSE THIS SCRIPT!
On the first script u should put this (put this on serverscriptservice and make sure its a serverscript):

game.ReplicatedStorage.testRemote.OnServerEvent:Connect(function(self, value)
self.leaderstats.Money.Value = value
end)

On the second script put this: (ServerScriptService)

local STARTER_MONEY = 10 -- Set the "10" to any number you wan't.

local get_data = function(userid)
	local suc = pcall(function() game:GetService("DataStoreService"):GetDataStore("Money"):GetAsync(userid) end)
	if suc then
		return game:GetService("DataStoreService"):GetDataStore("Money"):GetAsync(userid)
	else
		game:GetService("DataStoreService"):GetDataStore("Money"):SetAsync(userid, STARTER_MONEY)
		return STARTER_MONEY
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local money = Instance.new("NumberValue", leaderstats)
	money.Name = "Money"
	money.Value = get_data(player.UserId)  -- Do not worry; this will automically get player's money.

	money.Changed:Connect(function(v)
		task.wait(0.5) -- Keep datastores not losing perfomance.
		game:GetService("DataStoreService"):GetDataStore("Money"):SetAsync(player.UserId, v)
	end)
end)

On the command bar, run this command:
game.ReplicatedStorage.testRemote:FireServer(150)
Then leave and rejoin back to your game and look at your money stats.

Do you mean 2 seperate scripts or both of these inside one?

It must be seperate, both. Try this script out when it’s seperate.

Hi, this is this the Full Connection:

DSS = game:GetService("DataStoreService")
Store = DSS:GetDataStore("Money")

Key = "Player_" -- Key
Storage = {} -- For Storing Data

Template = { -- The Items the Data will Save
   Cash = 100;
}

game.Players.PlayerAdded:Connect(function(p)
local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue", leaderstats)
	Money.Name = "money"
	Money.Value = 0

local Success, Data = pcall(function() -- pcall
  return Store:GetAsync(Key..p.UserId) -- returns data
end)

if Success then -- If Accessed DataStores
   if Data then -- If Data found
   Storage[p.UserId] = Data
   else -- if Player has no Data
   Storage[p.UserId] = Template
   end
else -- If Failed to Access DataStores
-- maybe retry or kick the player?
end
Money.Value = Storage[p.UserId]["Cash"] -- Value will equal to Table data

Money:GetPropertyChangedSignal("Value"):Connect(function()
 Storage[p.UserId]["Cash"] = Money.Value -- Will Update the Table
end)

end)

image

But hey, this was my Error that I’ve posted, okay.

Okay, I’ve finally fixed it:

local Data = game:GetService("DataStoreService"):GetDataStore("randomthingdatalolxd")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("NumberValue",leaderstats)
	money.Name = "money"
	money.Value = 0
	
	local DataLoad = Data:GetAsync(tostring(player.UserId))
	
	local Response, Success = pcall(function()
		return DataLoad
	end)
	
	if Success then
		money.Value = DataLoad[1]
	elseif not Success then
		error("Failed to Load leaderstats. Response Dataload: "..Response)
	end	
	
	task.wait(0.4)
	
	money.Value += 100
end)

game.Players.PlayerRemoving:Connect(function(player)
	Data:SetAsync(tostring(player.UserId),{
		player.leaderstats.money.Value
	})
end)

Unless you do if Success and DataLoad then

image

damn im being ignored

Hello, i’ve tried this for the first time and it set my value to 100,then i edited it to a 1000 and rejoined. then it got stuck at 0.

I have no idea what caused that, because i spelled the “Money” value correctly.