So , i tried a new Method for the Data Store put now i get the error "ServerScriptService.HallowenGates:57: attempt to index nil with number"
Here's The Code:
local DataStoreService = game:GetService("DataStoreService") local dataStore = DataStoreService:GetDataStore("HalloweenGatesStore") -- This can be changed to whatever you want local function saveData(player) -- The functions that saves data local tableToSave = { player.HalloweenGates.PumpkinCave.Value; -- First value from the table player.HalloweenGates.GiantPumpkin.Value; -- Second value from the table player.HalloweenGates.HauntedEntrance.Value; -- Third player.HalloweenGates.HauntedBiome.Value --Fourthed } local success, err = pcall(function() dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save end) if success then -- If the data has been saved print("Data has been saved!") else -- Else if the save failed print("Data hasn't been saved!") warn(err) end end game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game -- // Assigning player stats // local HalloweenGates = Instance.new("Folder") HalloweenGates.Name = "HalloweenGates" HalloweenGates.Parent = player local PumpkinCave = Instance.new("BoolValue") PumpkinCave.Name = "Pumpkincave" PumpkinCave.Parent = HalloweenGates local GiantPumpkin = Instance.new("BoolValue") GiantPumpkin.Name = "GiantPumpkin" GiantPumpkin.Parent = HalloweenGates local HauntedEntrance = Instance.new("BoolValue") HauntedEntrance.Name = "HauntedEntrance" HauntedEntrance.Parent = HalloweenGates local HauntedBiome = Instance.new("BoolValue") HauntedBiome.Name = "HauntedBiome" HauntedBiome.Parent = HalloweenGates local data -- We will define the data here so we can use it later, this data is the table we saved local success, err = pcall(function() data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore end) if success then -- If there were no errors and player loaded the data PumpkinCave.Value = data[1] -- Set the Value to the first value of the table (data) GiantPumpkin.Value = data[2] -- Set the Value to the second value of the table (data) HauntedEntrance.Value = data[3]-- ^^ HauntedBiome.Value = data[4]-- ^^ else -- The player didn't load in the data, and probably is a new player print("The player has no data!") -- The default will be set to 0 end end) game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves local success, err = pcall(function() saveData(player) -- Save the data end) if success then print("Data has been saved") else print("Data has not been saved!") end end) game:BindToClose(function() -- When the server shuts down for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players local success, err = pcall(function() saveData(player) -- Save the data end) if success then print("Data has been saved") else print("Data has not been saved!") warn(err) end end end)
The error comes from data
on line 57 being nil
.
It should be noted that the pcall()
only fails if there's an error accessing the DataStore
: it can succeed even if you're calling GetAsync()
for a new player and there is no data for them! If that's the case, it will just return nil
.
To cover this case, you can assign a default table to data
on line 51 if it comes back as nil
.
-- Get the data from the datastore or assign a default table if there is none data = dataStore:GetAsync(player.UserId) or { false, false, false, false }