So I made a system to redeem codes and earn money but had to remove the earning money part because for some reason money doesn’t save (number is a test amount)
How do I get the points to save?
2 Likes
First, before you even do anything, I recommend these things if you want to learn DataStores for your game.
DevKing Tutorial and Roblox Data Store Article
(The DevKing Tutorial is an easier way to follow along and do it step by step.)
If you would like me to show you what you could do with your script, I’ve layed it out for you here with everything you need to know.
local DSS = game:GetService("DataStoreService") --First we need to get the DataStoreService, you can define this as anything you want.
local NewDataStore = DSS:GetDataStore("NewDataStore") --Second, we need to name our DataStore.
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Model", player) --Adds a model into the player
leaderstats.Name = "leaderstats" --Names it leaderstats
local Points = Instance.new("IntValue", leaderstats) --Creates an IntegerValue in the leaderstats so that you have a value to use
Points.Name = "Points" --Just the name of the value
Points.Value = 0 --How much you have
local PlayerUserID = player.UserId --For getting the data, we first need their userID, so we use this
local success, errormessage = pcall(function() --Now we want to get the data, so we check if it's a success or error because they might be a new player and might not have data.
Data = NewDataStore:GetAsync(PlayerUserID) --We define "Data" as we get the datastore. The UserID inside is saying "Hey, get the progress from this saved PlayerID!".
end)
if success then --If it's a success
player.leaderstats.Points.Value = Data --Then we change the value to the data, which can be any number.
end
end)
game.Players.PlayerRemoving:Connect(function(player) --Now we want to save data.
local PlayerUserID = player.UserId --We get the UserID again.
local Cash = player.leaderstats.Points.Value -- We define what the leaderstats is so we can mention it.
local success, errormessage = pcall(function() --We then want to check if it's an error or a success.
NewDataStore:SetAsync(PlayerUserID, Cash) --So we mention our datastore "NewDataStore". Then we use :SetAsync to set the values. Inside the parenthesis we mention the ([Who we save it for] -and [What we're saving]) Which is that we're saving it to the Player's UserID and we're saving the cash value.
end)
if success then --Checking if it's a success
print("DataSaved") --We'll say the data is saved
if errormessage then --If it errors
print("There was an error") --It prints there was an error in the output
warn(errormessage) --and it warns them of the error message. (This is for the developer to check the output only.
end
end)
If you have any questions, please ask. Please note this is as simple as it gets for DataStores.
3 Likes
thanks I will be sure to watch it!
1 Like