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

Why do i have a nil value at line 41 and 50?

Asked by 4 years ago
Edited 4 years ago
local DataStoreService = game:GetService("DataStoreService")
local PDS = DataStoreService:GetDataStore("PointSim1235")


game.Players.PlayerAdded:Connect(function(NewPlayer)

    local Key = "PDS-" .. NewPlayer.UserId

    local GetSave = PDS:GetAsync(Key)

    local leaderstats = Instance.new("Folder", NewPlayer)
    leaderstats.Name = "leaderstats"

    local points = Instance.new("IntValue", leaderstats)
    points.Name = "Points"

    local Rebirths = Instance.new("IntValue", leaderstats)
    Rebirths.Name = "Rebirths"
    Rebirths.Value = 1

    local PlayerStats = Instance.new("Folder", NewPlayer)
    PlayerStats.Name = "Playerstats"

    local ClickBonus = Instance.new("IntValue", PlayerStats)
    ClickBonus.Name = "ClickBonus"

    NewPlayer.CharacterAdded:Connect(function()
        local character = NewPlayer.Character
        local At1 = Instance.new("Attachment", character:WaitForChild("Head"))
        local At2 = Instance.new("Attachment", character:WaitForChild("HumanoidRootPart"))
        At1.Name = "At1"
        At2.Name = "At2"
    end)

    if GetSave then --load data
        points.Value = GetSave[1]
        Rebirths.Value = GetSave[2]
        ClickBonus.Value = GetSave[3]
        print("Data Loaded For " .. NewPlayer.Name)
    else
        points.Value = GetSave[0]
        Rebirths.Value = GetSave[1]
        ClickBonus.Value = GetSave[0]
        print("New Data Created For " .. NewPlayer.Name)
    end 
end)


game.Players.PlayerRemoving:Connect(function(player)
    print("Saving Data For " .. player.Name)

    local Key = "PDS-" .. NewPlayer.UserId

    local Stats1 = player.leaderstats
    local Stats2 = player.Playerstats

    local DataToSave = {}       
    for _, Stat in pairs(Stats1:GetChildren()) do
        table.insert(DataToSave, Stat.Value)
        warn("Player Left, Saving " .. Stat.Name .. " | " .. Stat.Value)
    end
    for _, Stat in pairs(Stats2:GetChildren()) do
        table.insert(DataToSave, Stat.Value)
        warn("Player Left, Saving " .. Stat.Name .. " | " .. Stat.Value)
    end

    PDS:SetAsync(Key, DataToSave)
end)
0
On line 50 you are using "NewPlayer", which can only be accessed in the scope of the Players.PlayerAdded event, use "player" instead climethestair 1663 — 4y
0
wait wut? can u explain that more clearly pls? xTheodxre 16 — 4y
0
ok thx it worked, but wut about line 41? xTheodxre 16 — 4y
0
There can't be a space when accessing a table value. PhantomVisual 992 — 4y
View all comments (2 more)
0
huh? xTheodxre 16 — 4y
0
im a noob at scripting so i don't really understand, srry for that xTheodxre 16 — 4y

Answer this question