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

How to set a sum of money in this DataStore script ?

Hello everyone, So, I copied code from DataStore and I would like to define a sum of money but my tests don't work, I don't know how to do it.

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 cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Parent = leaderstats
		
		local data
		local success, errormessage = pcall(function()
			data = myDataStore:GetAsync(Player.UserId.."-cash")
		end)
		if success then
			cash.Value = data
		else
			print("There was an error whilst getting your data")
			warn(errormessage)
		end
end)

game.Players.PlayerRemoving:Connect(function(Player)
		local success, errormessage = pcall(function()
			myDataStore:SetAsync(Player.UserId.."-cash", Player.leaderstats.Cash.Value)
		end)
		
		if success then
			print("Data successfully saved")
		else
			print("There was an error when saving data")
			warn(errormessage)
		end
		
end)

I tryed:

		local cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Parent = leaderstats
		cash.Value = 1000

But it doesn't work, help me please

1 answer

Hello, if by "define" you mean set a specific amount of currency to the player when you join the game, so for example, the player starts with a certain amount, add this to line 12 of your script;

cash.Value = 1000

If you don't mean this, and mean that you wish to test this datastore, then when you "run" or "server test" the game, open the Menu>Settings>(Scroll to the Bottom)Dev Consol>Server And on the "Command Line" at the bottom type the following line;

game.Players.USERNAME.cash.Value = NUM

Where USERNAME is your ROBLOX Username, and NUM is the amount.

Please comment below if you have any questions ,or if I didn't answer what you wished!

6 comments

0

Thank you for answering me and yes, I want when a Player enters the game, he has a certain amount of money so as you said I put cash.Value = 1000 But it doesn't work.

0

Try this: Making line 12 line 13, and putting "cash.Value = 1000" to line 11!

0

@BryanFehr It doesn't work, I don't understrand why it doesn't work

0

It may be due to fact that you have already saved 0 cash in datastore that gets overwritten. Change datastore name and test again

0

@karlo_tr10 I changed datastore name in line 2: local myDataStore = DataStoreService:GetDataStore("myDataStore"), but it doesn't work

0

Finally, I changed my script and it's work but thank you for you help

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