After changing the game DataStore a lot of times I’m unable to save data.
The weird thing is it used to work fine. I’m almost certain that I changed nothing in the code so this is beyond me.
Something noteworthy is that when I changed back to the original DataStore the data saved fine.
I have since forgotten the name of the original DataStore and I’m unable to save data.
Since I’m using a Testing Place for my game at the moment I hope when I switch to the main game the issue will resolve itself.
I have tried to find if somebody has encountered a similar problem and so far I have found nothing.
I’m unsure if this is a bug or my fault.
---Variables---
local DataStoreService = game:GetService("DataStoreService")
local GameDataStore = DataStoreService:GetDataStore("DataTest")
---Extract Numbers Function---
local function GetNumbers(String)
if String then
return string.match(String,"%d+")
end
end
---PlayerAdded Function--
local function PlayerAdded(Player)--This is not the hook
local PlayerInvClone = game.ReplicatedStorage:WaitForChild("PlayerInventory"):Clone()
PlayerInvClone.Parent = Player
local DataBoolClone = game.ReplicatedStorage.Values:WaitForChild("DataFullyLoaded"):Clone()
DataBoolClone.Parent = Player
if GameDataStore:GetAsync(Player) then
print"Player has data"
for i,v in pairs(GameDataStore:GetAsync(Player)) do
local StringValue = Instance.new("StringValue")
StringValue.Name = v[1]
StringValue.Value = v[2]
if GetNumbers(v.Name) then --Checks if string contains number
StringValue.Parent = Player.PlayerInventory.Items
elseif StringValue.Name == "Armor" then
StringValue.Parent = Player.PlayerInventory.Armor
elseif StringValue.Name == "Cosmetic" then
StringValue.Parent = Player.PlayerInventory.Cosmetics
elseif StringValue.Name == "Weapon" then
StringValue.Parent = Player.PlayerInventory.Weapons
end
end
Player.DataFullyLoaded.Value = true
else -- If player has no data
print"Player has no data"
end
while wait(30) do --DATA save loop to avoid data loss
if Player.DataFullyLoaded.Value == true then
local ToSave = {}
for i,v in pairs(Player.PlayerInventory.Armor:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
for i,v in pairs(Player.PlayerInventory.Cosmetics:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
for i,v in pairs(Player.PlayerInventory.Items:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
for i,v in pairs(Player.PlayerInventory.Weapons:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
GameDataStore:SetAsync(Player, ToSave)
print"Saved data?"
end
end
end
---Player Removed Function---
local function PlayerRemoved(Player) --Saves all Instances in inventory
if Player.DataFullyLoaded.Value == true then
local ToSave = {}
for i,v in pairs(Player.PlayerInventory.Armor:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
for i,v in pairs(Player.PlayerInventory.Cosmetics:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
for i,v in pairs(Player.PlayerInventory.Items:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
for i,v in pairs(Player.PlayerInventory.Weapons:GetChildren()) do
table.insert(ToSave, {v.Name, v.Value})
end
GameDataStore:SetAsync(Player, ToSave)
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoved)
Don’t mind my notes. If anybody knows what’s wrong enlighten me.