Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this script that saves a players stages?

Asked by 3 years ago

i made a script that is supposed to save a players stage when they leave the game. for some reason the script will not work this is the script:

local players = game:GetService("Players") local dataStoreService = game:GetService("DataStoreService") local saveDataStore = dataStoreService:GetDataStore("SaveDataTest")

local function saveplrData(plr)

local succes,err = pcall (function()

    local saveData = {}
    for _,stats in pairs (plr.leaderstats:GetChildren()) do


        saveData[stats.Name] = stats.Value

    end

    saveDataStore:SetAsync(plr.UserId,saveData)

end)

if not succes then return err end

end

players.PlayerAdded:connect(function(plr)

local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr

local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = stats

local data = saveDataStore:GetAsync(plr.UserId)

if data then

    print(data.Stage)

    for _,stat in pairs (stats:GetChildren()) do

        stat.Value = data[stat.Name]

    end

else

        print(plr.Name .. " has no data")

end

plr.CharecterAdded:connect(function()

    local humanoid,hrp = char:WaitForChild("Humanoid"),char:WaitForChild("HumanoidRootPart")

    wait()

    if humanoid and hrp then

        if stage.Value ~= 0 then

            local part = workspace.ObbyStages:FindFirstChild(Stage.Value)
            hrp.CFrame = part.CFrame + Vector3.new(0,1,0)

        end
    end

end

end)

players.PlayerRemoving:connect(function(plr)

local err = savePlrData(plr)

if err then print(err) end

end)

game:BindToClose(function()

    for_,plr in pairs (players:GetPlayers()) do 

        local err = savePlrData(plr)

        if err then print(err) end

    end

wait(2)

end)

1 answer

Log in to vote
0
Answered by
TNTIsLyfe 152
3 years ago
Edited 3 years ago

I think this is the problem, u cant insert in a table like that ill show u how

local succes,err = pcall (function()
local saveData = {}

    for _,stats in pairs (plr.leaderstats:GetChildren()) do


        table.insert(SaveData, stats.Name, stats.Value)

    end

    saveDataStore:SetAsync(plr.UserId,saveData)

end)

if not succes then return err end
0
i am sorry if it doesnt work, im kinda new to tables TNTIsLyfe 152 — 3y
0
ok i will try it venprince -2 — 3y
0
i changed it and it did not do anything venprince -2 — 3y
Ad

Answer this question