Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

attempt to index number with 'Stage'? (line 25) [closed]

Asked by 1 month ago
Edited 1 month ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.
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)

Closed as Non-Descriptive by JesseSong

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 1 month ago

"data" is going to return a table, to get index's from a table use [index] or ["index"] not "."

Ad