Hello, Developers.
I am making a DataStore system for a project and this pops up when I test:
API Services and HTTP Services are both enabled. Is this a bug with ROBLOX, or did I make a mistake in my code?
DataStore Module
local module = {}
--// DATASTORE
local DataStore = game:GetService("DataStoreService")
--// GET DATASTORES
local Player_Data = DataStore:GetDataStore("PlayerData", 1)
local ToolsOwned = DataStore:GetDataStore("ToolsOwned", 1)
local BackpacksOwned = DataStore:GetDataStore("BackpacksOwned", 1)
--// LOAD DATA FUNCTION
function module.LoadData(plr)
--// VARIABLES
local Key = plr.UserId -- Key for DataStore
local Stats = plr:WaitForChild("leaderstats") -- Player stats
local Coins = Stats:WaitForChild("Coins") -- Coins
local rPlayer = game:GetService("ReplicatedStorage").Players:WaitForChild(plr.Name) -- ReplicatedStorage Player
local Success, Err = pcall(function() -- Pcall incase an error occurs
--// REGULAR DATA
local PlayerData = Player_Data:GetAsync(Key) -- Gets Data
if PlayerData then -- If player data exists, then load it
Coins.Value = PlayerData.Coins
rPlayer.ToolEquipped.Value = PlayerData.ToolEquipped
rPlayer.BackpackEquipped.Value = PlayerData.BackpackEquipped
else -- If it doesn't, set to default
Coins.Value = 0
rPlayer.ToolEquipped.Value = "Starter Tool"
rPlayer.BackpackEquipped.Value = "Starter Backpack"
local ValuesToSave = {
Coins = Coins.Value,
ToolEquipped = rPlayer.ToolEquipped.Value,
BackpackEquipped = rPlayer.BackpackEquipped.Value
}
Player_Data:SetAsync(Key, ValuesToSave) -- Saves Data
end
--// TOOLS OWNED
local ToolsOwnedData = ToolsOwned:GetAsync(Key) -- Gets data
if ToolsOwnedData then -- If data exists then load it
for _,v in pairs(ToolsOwnedData) do -- Iterates through the data
local Tool = game:GetService("ReplicatedStorage").Tools:FindFirstChild(v)
if Tool then
local Temp = Instance.new("BoolValue")
Temp.Name = v
Temp.Parent = rPlayer.ToolsOwned
end
end
else -- If data does not exist, set to default
local Temp = Instance.new("BoolValue")
Temp.Name = "Starter Tool"
Temp.Parent = rPlayer.ToolsOwned
end
--// BACKPACKS OWNED
local BackpacksOwnedData = BackpacksOwned:GetAsync(Key) -- Gets data
if BackpacksOwnedData then -- If data exists then load it
for _,v in pairs(BackpacksOwnedData) do -- Iterates through the data
local Backpack = game:GetService("ReplicatedStorage").Backpacks:FindFirstChild(v)
if Backpack then
local Temp = Instance.new("BoolValue")
Temp.Name = v
Temp.Parent = rPlayer.BackpacksOwned
end
end
else -- If data does not exist, set to default
local Temp = Instance.new("BoolValue")
Temp.Name = "Starter Backpack"
Temp.Parent = rPlayer.BackpacksOwned
end
end)
if not Success then -- Checks if it errored
warn("Error while loading data for " .. plr.Name .. ": " .. Err) -- Send error message
else -- If it didn't error
print("Data for " .. plr.Name .. " loaded successfully.") -- Sends success message
end
--// END
end
--// SAVE DATA FUNCTION
function module.SaveData(plr)
--// VARIABLES
local Key = plr.UserId -- Key for DataStore
local Stats = plr:WaitForChild("leaderstats") -- Player stats
local Coins = Stats:WaitForChild("Coins") -- Coins
local rPlayer = game:GetService("ReplicatedStorage").Players:WaitForChild(plr.Name) -- ReplicatedStorage Player
local Success, Err = pcall(function() -- Pcall incase an error occurs
--// REGULAR DATA
local ValuesToSave = {
Coins = Coins.Value,
ToolEquipped = rPlayer.ToolEquipped.Value,
BackpackEquipped = rPlayer.BackpackEquipped.Value
}
Player_Data:SetAsync(Key, ValuesToSave) -- Saves Data
--// TOOLS OWNED
local ToolsOwnedTable = {} -- Creates a table for tools to be stored in
for _,v in pairs(rPlayer.ToolsOwned:GetChildren()) do -- Iterates through the player's tools
if v and game:GetService("ReplicatedStorage").Tools:FindFirstChild(v.Name) then -- Checks if tool exists
table.insert(ToolsOwnedTable, v.Name) -- Inserts it to a table
end
end
ToolsOwned:SetAsync(Key, ToolsOwnedTable) -- Saves Data
--// BACKPACKS OWNED
local BackpacksOwnedTable = {} -- Creates a table for tools to be stored in
for _,v in pairs(rPlayer.BackpacksOwned:GetChildren()) do -- Iterates through the player's tools
if v and game:GetService("ReplicatedStorage").BackpacksOwned:FindFirstChild(v.Name) then -- Checks if tool exists
table.insert(BackpacksOwnedTable, v.Name) -- Inserts it to a table
end
end
BackpacksOwned:SetAsync(Key, BackpacksOwnedTable) -- Saves Data
end)
if not Success then -- Checks if it errored
warn("Error while saving data for " .. plr.Name .. ": " .. Err) -- Send error message
else -- If it didn't error
print("Data for " .. plr.Name .. " saved successfully.") -- Sends success message
end
--// END
end
--// PLAYER LEFT FUNCTION
function module.PlayerLeft(rPlr)
rPlr:Destroy()
end
return module
Any contributions are greatly appreciated! (:
