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

My obby stage saving system doesn't teleport the player?

Asked by
kom297 -4
5 years ago

I've been working on this save system for a few hours however for some reason it throws me the error of "19:38:14.934 - ServerScriptService.playerHandler:43: attempt to index local 'part' (a nil value)" Hoping somebody will know why Here is the script:

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

local function savePlayerData(player)
    local success,err = pcall(function()
        local savedata = {}
        for _,stat in pairs(player.leaderstats:GetChildren()) do
            savedata[stat.Name] = stat.Value
        end
        saveDataStore:SetAsync(player.UserId,savedata)
    end)
    if not success then return err end
end

players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"
    stats.Parent = player

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

    local data = saveDataStore:GetAsync(player.UserId)

    if data then
        print(data.Stage)

        for _,stat in pairs(stats:GetChildren()) do
            stat.Value = data[stat.Name]
        end
    else
        print(player.Name .. " has no data")
    end

    player.CharacterAdded:connect(function(char)
        local hum,hrp = char:WaitForChild("Humanoid"),char:waitForChild("HumanoidRootPart")
        wait()
        if hum and hrp then
            if stage.Value ~= 0 then
                local part = workspace.obbyStages:FindFirstChild(stage.Name)
                hrp.CFrame = part.CFrame + Vector3.new(0,1,0)
            end
        end
    end)
end)

players.PlayerRemoving:connect(function(player)
    local err = savePlayerData(player)
    if err then print(err) end
end)

game:BindToClose(function()
    for _,plr in pairs(players:GetPlayers()) do
        local err = savePlayerData(player)
        if err then print(err) end
    end
    wait(2)
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

*Assuming your saving script works

Instead of using (line 38)

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

try using

repeat wait() until char.Humanoid
repeat wait() until char.HumanoidRootPart
local hum,hrp = char:FindFirstChild("Humanoid"),char:FindFirstChild("HumanoidRootPart")
0
i'll try thank you kom297 -4 — 5y
0
i mean that stopped the original error but now it throws the error " 16:59:21.817 - ServerScriptService.playerHandler:45: attempt to index local 'part' (a nil value)" kom297 -4 — 5y
0
In line 42, you find the stage in workspace, but you're using the IntValue NAME. Change that to local part = workspace.obbyStages:FindFirstChild(stage.Value) Brandon1881 721 — 5y
Ad

Answer this question