So, i'm making an achievement list, and there is a value which i'm going to make that you earn an achievement when the value gets to 3. When the value changes from 0, I leave, then rejoin, the value goes back to 0. Can anyone explain why this happens?
local DataStore = game:GetService("DataStoreService") local ds3 = DataStore:GetDataStore("Value2") game.Players.PlayerAdded:connect(function(player) wait(1) local folder = player.BadgeFolder local badge_1 = Instance.new("IntValue",folder.Badge1) badge_1.Name = "Badge_1" badge_1.Value = ds3:GetAsync(player.UserId) or 0 ds3:SetAsync(player.UserId, badge_1.Value) badge_1.Value.Changed:connect(function() ds3:SetAsync(player.UserId, badge_1.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds3:SetAsync(player.UserId, player.BadgeFolder.Badge1.Badge_1.Value) end)
The reason for this is because of line 11.
badge_1.Value.Changed:connect(function()
You are trying to index a number value, which will throw an error. You should instead be doing badge_1.Changed
. However it is not necessary to call SetAsync
on every change, as if it constantly changes, you may throttle the request limit.
local DataStore = game:GetService("DataStoreService") local ds3 = DataStore:GetDataStore("Value2") game.Players.PlayerAdded:Connect(function(player) -- connect is deprecated use Connect local folder = player.BadgeFolder local badge_1 = Instance.new("IntValue") -- parent argument is deprecated badge_1.Name = "Badge_1" badge_1.Value = ds3:GetAsync(player.UserId) or 0 badge_1.Parent = folder.Badge1 end) game.Players.PlayerRemoving:Connect(function(player) ds3:SetAsync(player.UserId, player.BadgeFolder.Badge1.Badge_1.Value) end)
And finally, your code is extremely messy. It is important to indent your code, and with consistency. Though it will not affect the result of your code, it is important so that you and others can read it.