local DataStoreService = game:GetService("DataStoreService")
local CheckpointDataStore = DataStoreService:GetDataStore("CheckpointDS")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = leaderstats
local Won = Instance.new("BoolValue")
Won.Name = "Won"
Won.Parent = player
local playerUserId = "Key_"..player.UserId
local data
local success, errorMessage = pcall(function()
data = CheckpointDataStore:GetAsync(playerUserId, data)
end)
if success then
Stage.Value = data.Stage
Won.Value = data.Won
print("Successfully Loaded Player Data!")
end
game.ReplicatedStorage.SaveData.OnServerEvent:Connect(function(player)
print("Saved "..player.Name.."'s Data!")
local success, errorMessage = pcall(function()
CheckpointDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Successfully Saved "..player.Name.."'s Data!")
else
warn(errorMessage)
end
end)
while true do wait(30)
local WaitTime = math.random(2,5)
for i,v in pairs(game.Players:GetPlayers()) do
if v.PlayerGui.SaveData.SaveMsg.Visible == false then
v.PlayerGui.SaveData.SaveMsg.Visible = true
v.PlayerGui.SaveData.SaveMsg.Text = "Saving Data..."
v.PlayerGui.SaveData.SaveMsg.TextColor3 = Color3.fromRGB(255,255,0)
end
end
print("Saving Data...")
wait(WaitTime)
print("Successfully Auto Saved Data!")
CheckpointDataStore:SetAsync(playerUserId, player.leaderstats.Stage.Value)
for i,allplayers in pairs(game.Players:GetPlayers()) do
allplayers.PlayerGui.SaveData.SaveMsg.Text = "Successfully Saved Data!"
allplayers.PlayerGui.SaveData.SaveMsg.TextColor3 = Color3.fromRGB(85,255,0)
end
wait(2)
for i,allplayers in pairs(game.Players:GetPlayers()) do
allplayers.PlayerGui.SaveData.SaveMsg.Text = ""
allplayers.PlayerGui.SaveData.SaveMsg.TextColor3 = Color3.fromRGB(115,115,115)
allplayers.PlayerGui.SaveData.SaveMsg.Visible = false
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Key_"..player.UserId
local data = {
Stage = player.leaderstats.Stage.Value;
Won = player.Won.Value;
}
local success, errorMessage = pcall(function()
CheckpointDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Player Data Successfully Saved!")
else
print("There was an error whilst saving data!")
warn(errorMessage)
end
end)
✓ ANSWER ACCEPTED
attempt to index number with 'Stage'? (line 25)
1 answer
✓ Accepted answer
"data" is going to return a table, to get index's from a table use [index] or ["index"] not "."
This discussion is preserved as it was. New answers, comments, and votes are closed.
