Saving money (datastore)

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]
		print("Successively Loaded leaderstats!")
	elseif not Success then
		error("Failed to Load leaderstats.")
	end	

	task.wait(0.4)
	
	money.Value += 100

	while task.wait(0.36) do
		money.Value += 100
	end
end)

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

Edit: Edited the Script: Added a Print Statement Of the Line 19.

Please wait a minute, I will try to rewrite this and then send it here.

@x8yv, you can try my script, it works for me.
If it doesn’t work for you, I have 0 words to solve this topic.

Try this script:

local ds = game:GetService("DataStoreService")
create = function(classname, properties)
	local i = Instance.new(classname)
	for _,v in pairs(properties) do
		i[_] = v
	end
	return i
end
local remote = create("RemoteEvent", {Name = "test", Parent = game.ReplicatedStorage})
remote.OnServerEvent:connect(function(self, val)
	self.leaderstats.Money.Value = val
end)
game.Players.PlayerAdded:Connect(function(player)
	local stats = create("Configuraion", {Parent = player, Name = "leaderstats"})
	local Money = create("IntValue", {Parent = stats, Value = ds:GetDataStore("Money"):GetAsync(player.UserId) or 0, Name = "Money"})
	Money.Changed:Connect(function(v)
		ds:GetDataStore("Money"):SetAsync(player.UserId, v)
	end)
end)

On client, wait like 2 secods and then on the command bar run this command:
game.ReplicatedStorage.test:FireServer(90)
After that is ran, wait another second and try rejoining. It actually works for me, so if this errors I don’t know what to do. No need to set the scripts seperate.
But after you’re done testing, delete those lines:

local remote = create("RemoteEvent", {Name = "test", Parent = game.ReplicatedStorage})
remote.OnServerEvent:connect(function(self, val)
	self.leaderstats.Money.Value = val
end)

this is your script

Try my script and tell me if it works.

I’ve made some modifications on 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]
		print("Successively Loaded leaderstats!")
	elseif not Success then
		error("Failed to Load leaderstats.")
	end	

	task.wait(0.4)
	
	money.Value += 100

	while task.wait(0.36) do
		money.Value += 100
	end
end)

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

I think this works! The money went up to 1.9k and when I rejoined it instantly started going from 1.9k

Sorry wrong reply ersddddddddddd

I reccommend to remove the money.Value, because people will receive money just by doing nothing if you use his script. (the while task.wait)

Can you please send me the script where you dont get 100 money every second? Thanks.

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]
		print("Successively Loaded leaderstats!")
	elseif not Success then
		error("Failed to Load leaderstats.")
	end	
end)

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

Add a part in workspace, add a ClickDetector inside of the Part on it and copy this script on it but Outside Of The ClickDetector:

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	Player.leaderstats.money.Value += 1
end)
1 Like

Thank you very much, and i thank everyone else for putting up with me :slight_smile:

2 Likes

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