Hello! The code below works except for the PlayerRemoving function. It's in the ServerScriptService and it saves two values "Trimmings" and "Manliness" for a given scope. These values are based on the values off a player leaderboard. There are several issues I've found with it and I just can't seem to get it to work! ;c
local DataStore = game:GetService("DataStoreService")
local Prefix = "User_"
game.Players.PlayerAdded:connect(function(Player)
local Leaderboard = Instance.new ("IntValue", Player)
Leaderboard.Name = "leaderstats"
local Trimmings = Instance.new ("NumberValue", Leaderboard)
Trimmings.Name = "Trimmings"
local Manliness = Instance.new ("NumberValue", Leaderboard)
Manliness.Name = "Manliness"
local key = (Prefix .. Player.UserId)
local PlayerData = DataStore:GetDataStore("Data", key)
local Data = PlayerData:GetAsync(key)
if not Data then
Data = {Trimmings = 0, Manliness = 0}
PlayerData:SetAsync(key, Data)
end
--print(key)
Trimmings.Value = Data.Trimmings or 0
Manliness.Value = Data.Manliness or 0
end)
game.Players.PlayerRemoving:connect(function(Player)
local key = (Prefix .. Player.UserId)
local Data = DataStore:GetDataStore("Data", key)
print(key .. "b")
print(Player.leaderstats.Trimmings.Value)
Data:UpdateAsync(key, {Trimmings = Player.leaderstats.Trimmings.Value, Manliness = Player.leaderstats.Manliness.Value})
print(key .. "a")
end)
while wait(200) do
for _,Player in pairs(game.Players:GetChildren()) do
local key = (Prefix .. Player.UserId)
local Data = DataStore:GetDataStore("Data", key)
print(Player.leaderstats.Trimmings.Value)
Data:UpdateAsync(key, function(value)
return {Trimmings = Player.leaderstats.Trimmings.Value, Manliness = Player.leaderstats.Manliness.Value}
end)
end
end
**Issue 1: ** In Line 36, when I use UpdateAsync I get a Cannot cast value to function error.
**Issue 2: ** In the same line, when I use SetAsync I get no errors, but it doesn't execute the code and doesn't do the print(key .. "a") I put as a check. ;c
**Issue 3: ** Even though the loop that saves the data of everyone every 200 seconds works, the code block doesn't work when I turn it into a function ;c
**Issue 4: ** Even if I try to save Trimmings and Manliness individually, I still get the same issues.
aaaa so I guess thanks for the help! If you know how to fix it please do explain it to me as well!! ; o ;
