How Do I Make A Datastore For A Leaderstat Named Cash

How Do I Make A Datastore For A Leaderstat Named Cash

I Have Made A Learderstat Which Is Cash And Ive Been Watching Tutorials And Trying Everything and it just aint saving.

This Script is a robbery script and when players finish they get cash.
how do i make this save ?
here is the script

local maxCash = 2500
local cashPerSecond = 100
local timeToRob = 100
local robbing = false
local cooldown = 500
local closed = false
local defaultVaultPosition = script.Parent.VaultDoor.Position

local playersRobbing = {}
local alreadyRobbed = {}
local guis = {}

function endRobbery()

closed = true
robbing = false

wait(1)

script.Parent.Door.CanCollide = true
script.Parent.VaultDoor.Position = defaultVaultPosition


for i, gui in pairs(guis) do
	gui:Destroy()
end

for p, x in pairs(playersRobbing) do
	p.Character.HumanoidRootPart.CFrame = script.Parent.FailedToRob.CFrame
end


playersRobbing = {}
alreadyRobbed = {}

script.Alarm:Stop()

wait(cooldown)
closed = false

script.Parent.Door.CanCollide = false

end

function beginRobbery()

for i = 1, timeToRob do
	
	wait(1)
	
	local region3 = Region3.new(script.Parent.VaultArea.Position - script.Parent.VaultArea.Size/2, script.Parent.VaultArea.Position + script.Parent.VaultArea.Size/2)
	local parts = workspace:FindPartsInRegion3(region3, script.Parent, math.huge)
	
	local playersGivenCashTo = {}
	
	for x, part in pairs(parts) do
		
		if playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] < maxCash and not playersGivenCashTo[game.Players:GetPlayerFromCharacter(part.Parent)] then
			
			playersGivenCashTo[game.Players:GetPlayerFromCharacter(part.Parent)] = true
			
			playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] += cashPerSecond
			
			if not game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui:FindFirstChild("RobberyGui") then
				script.RobberyGui:Clone().Parent = game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui
				game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.Background.RobbedAmount.Text = "$0"
				table.insert(guis, game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui)
			end
			
			game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.Background.RobbedAmount.Text = "$" .. playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)]
		end
	end
end

endRobbery()

end

game.Players.PlayerAdded:Connect(function(p)

local ls = Instance.new("Folder", p)
ls.Name = "leaderstats"

local cash = Instance.new("IntValue", ls)
cash.Name = "Cash"

end)

local touchedCooldown = {}

script.Parent.Door.Touched:Connect(function(hit)

if not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and not playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] and not alreadyRobbed[game.Players:GetPlayerFromCharacter(hit.Parent)] then
	
	script.Alarm:Resume()
	
	touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] = true

	playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] = 0
	
	spawn(function()
		wait(2)
		touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] = nil
	end)
	
	if not robbing then
		beginRobbery()
	end
	robbing = true

	
elseif not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] then
	
	game.Players:GetPlayerFromCharacter(hit.Parent).leaderstats.Cash.Value += playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)]
	
	playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] = nil
	alreadyRobbed[game.Players:GetPlayerFromCharacter(hit.Parent)] = true
	
	
	local stillRobbing = false
	for p, x in pairs(playersRobbing) do
		if p[x] then stillRobbing = true end
	end
	
	if not stillRobbing then
		
		endRobbery()
	end
end

end)

script.Parent.OpenVault.Touched:Connect(function(hit)

if game.Players:GetPlayerFromCharacter(hit.Parent) then
	
	wait(3)
	
	game:GetService("TweenService"):Create(script.Parent.VaultDoor, TweenInfo.new(5), {Position = defaultVaultPosition + Vector3.new(0, script.Parent.Door.Size.Y, 0)}):Play()
end

end)

for i, laser in pairs(script.Parent.Lasers:GetChildren()) do

local touched = {}

laser.Touched:Connect(function(hit)
	
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if plr and not touched[plr] then 
		
		touched[plr] = true
		
		hit.Parent.Humanoid:TakeDamage(20)
		
		wait(1)
		
		touched[plr] = nil
	end
end)

end

From This Script How Do I Make Cash Save ?

Wait, so I don’t get what your trying to do I mean I get you need a datastore but right here just looks like your trying to do something else.

What I can do is give you a datastore to go off of but is that all you need? I mean it seems like your title and your scripts aren’t matching up.

This is what I have just edit the names of it and it’s pretty much good to go.

local CoinName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("Change") -- Change this to what ever you want, changing it again will wipe any data.
game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "Currency"
	folder.Parent = player	

	local Coin = Instance.new("NumberValue")
	Coin.Name = CoinName
	Coin.Parent = folder

	local ID = CoinName.."-"..player.UserId
	local savedData = nil	

	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)

	if savedData ~= nil then
		Coin.Value = savedData
		print("Data loaded")
	else
		-- New player
		Coin.Value = 0
		print("New player to the game")
	end


end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = CoinName.."-"..player.UserId
	DataStore:SetAsync(ID,player.Currency.Coins.Value)
end)

game:BindToClose(function()



	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end

	wait(5)	

end)


Merged Datastore into your script...
local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("LeaderstatsData1")
local Players = game:GetService("Players")

local function PlayerAdded(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local Cash = Instance.new("NumberValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats

	local PlayerKey = tostring("Player_"..Player.UserId)
	local Data
	pcall(function()
		Data = DataStore:GetAsync(PlayerKey)
	end)

	Cash.Value = Data and Data["Cash"] or 0
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(Player)
	local StatFolder1 = Player:FindFirstChild("leaderstats")
	if StatFolder1 then
		local PlayerKey = tostring("Player_"..Player.UserId)
		local Data = {}
		for i,v in pairs(StatFolder1:GetChildren()) do
			pcall(function()
				Data[v.Name] = v.Value
			end)
		end
		if #Data > 0 then
			pcall(function()
				DataStore:SetAsync(PlayerKey, Data)
			end)
		end
	end
end)

local maxCash = 2500
local cashPerSecond = 100
local timeToRob = 100
local robbing = false
local cooldown = 500
local closed = false
local defaultVaultPosition = script.Parent.VaultDoor.Position

local playersRobbing = {}
local alreadyRobbed = {}
local guis = {}

function endRobbery()

	closed = true
	robbing = false

	wait(1)

	script.Parent.Door.CanCollide = true
	script.Parent.VaultDoor.Position = defaultVaultPosition


	for i, gui in pairs(guis) do
		gui:Destroy()
	end

	for p, x in pairs(playersRobbing) do
		p.Character.HumanoidRootPart.CFrame = script.Parent.FailedToRob.CFrame
	end


	playersRobbing = {}
	alreadyRobbed = {}

	script.Alarm:Stop()

	wait(cooldown)
	closed = false

	script.Parent.Door.CanCollide = false
end

function beginRobbery()

	for i = 1, timeToRob do

		wait(1)

		local region3 = Region3.new(script.Parent.VaultArea.Position - script.Parent.VaultArea.Size/2, script.Parent.VaultArea.Position + script.Parent.VaultArea.Size/2)
		local parts = workspace:FindPartsInRegion3(region3, script.Parent, math.huge)

		local playersGivenCashTo = {}

		for x, part in pairs(parts) do

			if playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] < maxCash and not playersGivenCashTo[game.Players:GetPlayerFromCharacter(part.Parent)] then

				playersGivenCashTo[game.Players:GetPlayerFromCharacter(part.Parent)] = true

				playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] += cashPerSecond

				if not game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui:FindFirstChild("RobberyGui") then
					script.RobberyGui:Clone().Parent = game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui
					game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.Background.RobbedAmount.Text = "$0"
					table.insert(guis, game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui)
				end

				game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.Background.RobbedAmount.Text = "$" .. playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)]
			end
		end
	end

	endRobbery()
end

local touchedCooldown = {}

script.Parent.Door.Touched:Connect(function(hit)

	if not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and not playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] and not alreadyRobbed[game.Players:GetPlayerFromCharacter(hit.Parent)] then

		script.Alarm:Resume()

		touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] = true

		playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] = 0

		spawn(function()
			wait(2)
			touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] = nil
		end)

		if not robbing then
			beginRobbery()
		end
		robbing = true


	elseif not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] then

		game.Players:GetPlayerFromCharacter(hit.Parent).leaderstats.Cash.Value += playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)]

		playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] = nil
		alreadyRobbed[game.Players:GetPlayerFromCharacter(hit.Parent)] = true


		local stillRobbing = false
		for p, x in pairs(playersRobbing) do
			if p[x] then stillRobbing = true end
		end

		if not stillRobbing then

			endRobbery()
		end
	end
end)

script.Parent.OpenVault.Touched:Connect(function(hit)

	if game.Players:GetPlayerFromCharacter(hit.Parent) then

		wait(3)

		game:GetService("TweenService"):Create(script.Parent.VaultDoor, TweenInfo.new(5), {Position = defaultVaultPosition + Vector3.new(0, script.Parent.Door.Size.Y, 0)}):Play()
	end
end)

for i, laser in pairs(script.Parent.Lasers:GetChildren()) do

	local touched = {}

	laser.Touched:Connect(function(hit)

		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

		if plr and not touched[plr] then 

			touched[plr] = true

			hit.Parent.Humanoid:TakeDamage(20)

			wait(1)

			touched[plr] = nil
		end
	end)
end

game:BindToClose(function()
	wait(5)
end)

Make sure to turn on ‘Enable Studio Access to API Service’ in Game Settings > Security
Sorry, this was posted late… My internet was being reset.

Also, if you want to learn how to use datastores yourself, visit: Datastores!

2 Likes

would this work ? if i replaced the script with that ?

1 Like

ill give this a try hopefully it works

i want to save the Cash In It. so players have it when they rejoin.

I’m pretty sure it does work, if it doesn’t just replace it with the old one.

1 Like

doesnt work.
i replaced the old one.

Put the one I made in ServerScriptService, try that.

1 Like

i got it working but how do i make a tool shop for Cash and it save ?

Try using something I made not too long ago:

Just sidenote, profileservice is wayyy better than vanilla datastores as it features session-locking. As a beginner, u can use ur method howeever as u develop further, profileservice is the best choice.

Fyi, this is juz a suggestion.