So i made this datastore script which is supposed to save every value in the script but when i join for the first time all the values are true even tho i made it so that when someone joins for the first time the values would be false and, it dosent save them when they are edited.
How do i fix this issue?
Script:
local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("NOPE")
local success, err = pcall(function()
game.Players.PlayerAdded:Connect(function(plr)
wait(5)
local Cars = Instance.new("Folder")
Cars.Name = "OwnedCars"
Cars.Parent = plr
--------------------------------------------------------------------------------1
local DAFERRAOwned = Instance.new("BoolValue")
DAFERRAOwned.Name = "DAFERRAOwned"
DAFERRAOwned.Value = false
DAFERRAOwned.Parent = Cars
DAFERRAOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.DAFERRAOwned.Value)
end)
--------------------------------------------------------------------------------2
local DAFERRARTOwned = Instance.new("BoolValue")
DAFERRARTOwned.Name = "DAFERRARTOwned"
DAFERRARTOwned.Value = false
DAFERRARTOwned.Parent = Cars
DAFERRARTOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.DAFERRARTOwned.Value)
end)
--------------------------------------------------------------------------------3
local DAVENERAOwned = Instance.new("BoolValue")
DAVENERAOwned.Name = "DAVENERAOwned"
DAVENERAOwned.Value = false
DAVENERAOwned.Parent = Cars
DAVENERAOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.DAVENERAOwned.Value)
end)
--------------------------------------------------------------------------------4
local HEKTORMEDIONAOwned = Instance.new("BoolValue")
HEKTORMEDIONAOwned.Name = "HEKTORMEDIONAOwned"
HEKTORMEDIONAOwned.Value = false
HEKTORMEDIONAOwned.Parent = Cars
HEKTORMEDIONAOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.HEKTORMEDIONAOwned.Value)
end)
--------------------------------------------------------------------------------5
local MOUNTSRAKEOwned = Instance.new("BoolValue")
MOUNTSRAKEOwned.Name = "MOUNTSRAKEOwned"
MOUNTSRAKEOwned.Value = false
MOUNTSRAKEOwned.Parent = Cars
MOUNTSRAKEOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.MOUNTSRAKEOwned.Value)
end)
--------------------------------------------------------------------------------6
local NOVENAACESERAOwned = Instance.new("BoolValue")
NOVENAACESERAOwned.Name = "NOVENAACESERAOwned"
NOVENAACESERAOwned.Value = false
NOVENAACESERAOwned.Parent = Cars
NOVENAACESERAOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.NOVENAACESERAOwned.Value)
end)
--------------------------------------------------------------------------------7
local ROLANDGTROwned = Instance.new("BoolValue")
ROLANDGTROwned.Name = "ROLANDGTROwned"
ROLANDGTROwned.Value = false
ROLANDGTROwned.Parent = Cars
ROLANDGTROwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.ROLANDGTROwned.Value)
end)
--------------------------------------------------------------------------------8
local JELANSTERBETAOwned = Instance.new("BoolValue")
JELANSTERBETAOwned.Name = "JELANSTERBETAOwned"
JELANSTERBETAOwned.Value = false
JELANSTERBETAOwned.Parent = Cars
JELANSTERBETAOwned.Value = ds1:GetAsync(plr.UserId) or false
game.Players.PlayerRemoving:Connect(function(plr)
ds1:SetAsync(plr.UserId, Cars.JELANSTERBETAOwned.Value)
end)
end)
---------------------------------------------------------------------------------Cash
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder", player)
leader.Name = "leaderstats"
local Cash = Instance.new("IntValue", leader)
Cash.Name = "Cash"
Cash.Value = ds1:GetAsync(player.UserId) or 0
ds1:SetAsync(player.UserId, Cash.Value)
wait(5)
--Cash.Changed:connect(function()
print("SavingData")
ds1:SetAsync(player.UserId, Cash.Value)
end)
--end)
game.Players.PlayerRemoving:connect(function(player)
local SavedData = Instance.new("BindableEvent")
ds1:setAsync(player.UserId, player.leaderstats.Cash.Value)
ds1:setAsync(player.UserId, player.Cars.DAFERRAOwned.Value)
SavedData:Fire()
game:BindToClose(function()
SavedData.Event:Wait()
end)
end)
end)
