ROBLOX DataStore Issue

I’m having some issue with this script. It’s being used to save the players Gold and Level. Whenever I join the game the set value for the level is 1 and it’s not making it one its staying as 0 I have HTTP enabled along with all the other things but It’s still not working. I’m getting zero errors and all of my prints aren’t even showing up on the output. It’s in a Script located in ServerScriptStorage

local DataStoreService = game:GetService(“DataStoreService”)
local LevelDataStore = DataStoreService:GetDataStore(“Level”)
local GoldDataStore = DataStoreService:GetDataStore(“Gold”)

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

local level = Instance.new("NumberValue", leaderstats)
level.Name = "Level"
level.Value = 1

local gold = Instance.new("NumberValue", leaderstats)
gold.Name = "Gold"
gold.Value = 0

local levelData
local goldData


local success, errorMessage = pcall(function()
	local levelData = LevelDataStore:GetAsync(player.UserId)
	local goldData = GoldDataStore:GetAsync(player.UserId)
end)

if success then
	level.Value = levelData
	gold.Value = goldData
else
	level.Value = 1
	gold.Value = 0
	print(player.Name " : ".. player.UserId "'s DATA WAS NOT FOUND!")
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
LevelDataStore:SetAsync(player.leaderstats.Level.Value, player.UserId)
GoldDataStore:SetAsync(player.leaderstats.Gold.Value, player.UserId)
end)

if success then
	print(player.Name .. " : "..player.UserId .."'s DATA HAS BEEN FOUND!")
end

end)

1 Like

How would I save a Number Value to a data store? - Help and Feedback / Scripting Support - DevForum | Roblox

I made this script and I can’t seem to figure out how to make it work…

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

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

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

local level = Instance.new("IntValue", leaderstats)
level.Name = "Level"

local gold = Instance.new("IntValue", leaderstats)
gold.Name = "Gold"

local playerUserId = "Player_"..player.UserId

-- LOADING DATA

local data
local success, errorMessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)

if success then
	level.Value = data
	gold.Value = data
else
	level.Value = 1
	gold.Value = 0
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = {player.leaderstats.Level.Value, player.leaderstats.Gold.Value} -- THIS IS THE ISSUE

local success, errorMessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)
if success then
	print('Data successfully saved!')
else
	print('Data was not saved!')
	warn(errorMessage)
end

end)

When you assign the values in the playeradded you are attempting to assign a table to each
You’d have to get each index so instead of doing

level.Value = data
gold.Value = data

You would do

level.Value = data[1]
gold.Value = data[2]

Thank you! I figure it out, Glad to know some people are able to see my post and help out haha