Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I go about separating values from a DataStore?

Asked by 4 years ago

I'm fairly new to DataStore; I have managed to create a DataStore that saves and loads money, but now I want to try and save BoolValues. But instead of one value being saved, I want to save multiple...

At the moment, it doesn't work and shows no errors... Please could someone explain where I have gone wrong and show me how to correct it please?

Obviously, i've only tried two values for now rather than adding them all, just so i could see if my methods worked or not.. And they don't!

local Players = game:GetService("Players")
local TestWeapon = game:GetService("DataStoreService"):GetDataStore("TestWeapons")


Players.PlayerAdded:Connect(function(player)
theirid = player.UserId
FileName = "User"..player.UserId
local playerKey = "Player009" .. player.UserId
A = Instance.new("Folder")
A.Name = "User"..player.UserId
A.Parent = player

Item1 = Instance.new("BoolValue")
Item1.Name = "Sword"
Item1.Parent = A

Item2 = Instance.new("BoolValue")
Item2.Name = "Gun"
Item2.Parent = A


local success, err = pcall(function()
Val = TestWeapon:GetAsync(playerKey)
end)

if success then
  Item1.Value = Val
  Item2.Value = Val
else
end

--Change in Item1
local object = Item1
local function onChanged(property)
  TestWeapon:SetAsync(playerKey, Item1.Value, Item2.Value)
end
object.Changed:Connect(onChanged)


--Change in Item2
local object2 = Item2
local function onChanged(property)
  TestWeapon:SetAsync(playerKey, Item1.Value, Item2.Value)
end
object2.Changed:Connect(onChanged)

end)

Thank you!

Answer this question