I made a datastore for my game and I want to make it so that when a boolean value or a number value in the players stats changes, it gets saved. Heres the code:
local dataStoreService = game:GetService("DataStoreService")
local yuanData = dataStoreService:GetDataStore("YuanData")
local killsData = dataStoreService:GetDataStore("KillsData")
local fourthData = dataStoreService:GetDataStore("FourthData")
local defenseData = dataStoreService:GetDataStore("DefenseData")
local ultData = dataStoreService:GetDataStore("UltData")
local dashData = dataStoreService:GetDataStore("DashData")
local dbData = dataStoreService:GetDataStore("DoubleJumpData")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("IntValue", plr)
leaderstats.Name = "Stats"
local yuan = Instance.new("IntValue", leaderstats)
yuan.Name = "Yuan"
yuan.Value = yuanData:GetAsync(plr.UserId) or 0
yuan.Changed:connect(function()
yuanData:SetAsync(plr.UserId,yuan.Value)
end)
local kills = Instance.new("IntValue", leaderstats)
kills.Name = "Kills"
kills.Value = killsData:GetAsync(plr.UserId) or 0
kills.Changed:connect(function()
killsData:SetAsync(plr.UserId,kills.Value)
end)
local element = Instance.new("StringValue", leaderstats)
element.Name = "Element"
element.Value = "None"
local can = Instance.new("Folder", plr)
can.Name = "CanBuy"
local fourth = Instance.new("BoolValue", can)
fourth.Name = "FourthMove"
fourth.Value = fourthData:GetAsync(plr.UserId) or false
fourth.Changed:connect(function()
fourthData:SetAsync(plr.UserId,fourth.Value)
end)
local defense = Instance.new("BoolValue", can)
defense.Name = "DefenseMove"
defense.Value = defenseData:GetAsync(plr.UserId) or false
defense.Changed:connect(function()
defenseData:SetAsync(plr.UserId,defense.Value)
end)
local ult = Instance.new("BoolValue", can)
ult.Name = "UltMove"
ult.Value = ultData:GetAsync(plr.UserId) or false
ult.Changed:connect(function()
ultData:SetAsync(plr.UserId,ult.Value)
end)
local dash = Instance.new("BoolValue", can)
dash.Name = "Dash"
dash.Value = dashData:GetAsync(plr.UserId) or false
dash.Changed:connect(function()
dashData:SetAsync(plr.UserId,dash.Value)
end)
local dbJump = Instance.new("BoolValue", can)
dbJump.Name = "DoubleJump"
dbJump.Value = dbData:GetAsync(plr.UserId) or false
dash.Changed:connect(function()
dashData:SetAsync(plr.UserId,yuan.Value)
end)
end)
The DataStore only works on my Yuans Value and if it only goes up, if it goes down the datastore doesn't work, and for everything else it doesn't work at all.
