I’m currently working on a rank system for my game and I have no idea why my script applies the data to the values but doesnt save the data when the value changed
My code:
local DSS = game:GetService("DataStoreService")
local dataStore = DSS:GetDataStore("Team_Ranks")
local function saveData(plr)
local e = script.Parent.Players:FindFirstChild(plr.Name)
local tableToSave = {
e["Alpha Operations Division"].Value;
e["Area Agency"].Value;
e["Insurgency X"].Value;
e["Internal Control Group"].Value;
e["Medical Department"].Value;
e["Mobile Task Force"].Value;
e["Scientific Department"].Value;
e["Security Department"].Value;
e["Site Intelligence"].Value;
}
local success, errorMessage = pcall(dataStore.SetAsync, dataStore, plr.UserId, tableToSave)
if success then
print("Data saved")
else
print("Data not saved")
end
end
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Wait()
local t = script.Parent.Players.Template:Clone()
t.Name = p.Name
t.Parent = script.Parent.Players
wait()
local data = nil
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(p.UserId)
end)
if success and data then
t["Alpha Operations Division"].Value = data[1]
t["Area Agency"].Value = data[2]
t["Insurgency X"].Value = data[3]
t["Internal Control Group"].Value = data[4]
t["Medical Department"].Value = data[5]
t["Mobile Task Force"].Value = data[6]
t["Scientific Department"].Value = data[7]
t["Security Department"].Value = data[8]
t["Site Intelligence"].Value = data[9]
else
print("Player has no data")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
wait()
script.Parent.Players:FindFirstChild(player.Name):Destroy()
end)
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
task.spawn(saveData, player)
end
end)
The script is a server script inside of serverscriptservice