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

Edited:attempt to index local 'plr' (a nil value) in server mode?

Asked by
Glistre -7
7 years ago
Edited 7 years ago

I cannot fix this: on line 89 below (marked) I get a nil value for plr error in Servermode

Could someone help me fix this error ? I have tried everything I can think of

Also, my Player 1 does not load the skin and gamepasses do not work in server mode or the game but I thought I would start by fixing this error.

local function Sep(table,plr)
    for i,v in pairs(table, game.Players:GetPlayers()) do--me -removes stop working error after fixing clientside no Character, serverside no plr
            plr = game:GetService('Players').LocalPlayer--me -removes plr a nil value error serverside

        local char = plr.Character or plr.CharacterAdded:Wait()--me -removes Character a nil value error clientside
        if type(v)=="userdata" then
            v:Clone().Parent = plr.Backpack
        elseif type(v)=="number" then
            game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name).Value = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name).Value + (v)
        else
            if v:sub(0,2) == "W:" then
                plr.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed + tonumber(v:sub(3))
            else
                --plr.Character.Humanoid.MaxHealth = v + 50
                --plr.Character.Humanoid.Health = v
                plr.Character.Humanoid.MaxHealth = plr.Character.Humanoid.MaxHealth + tonumber(v:sub(3))
                plr.Character.Humanoid.Health = plr.Character.Humanoid.Health
            end
        end
    end
end

local function SepQ(table,plr)
    for i,v in pairs(table, game.Players:GetPlayers()) do--me -removes stop working error after fixing clientside no Character, serverside no plr
        plr = game:GetService('Players'):GetPlayers()--me -removes plr a nil value error serverside
        local char = plr.Character or plr.CharacterAdded:Wait()--me -removes Character a nil value error clientside
        --local plr = game:GetService('Players'):GetPlayers()--me -removes plr a nil value error serverside
        if type(v)=="userdata" then
            v:Clone().Parent = plr.Backpack
        elseif type(v)=="number" then
            return
        else
            if v:sub(0,2) == "W:" then
                plr.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed + tonumber(v:sub(3))
            else
                plr.Character.Humanoid.MaxHealth = plr.Character.Humanoid.MaxHealth + tonumber(v:sub(3))
                plr.Character.Humanoid.Health = plr.Character.Humanoid.Health
            end
        end
    end
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
    print(receiptInfo.ProductId)
    if Items[receiptInfo.ProductId] then
        if type(GetName(receiptInfo.PlayerId)) == "string" then
            wait()
            local p5 = game.Players:FindFirstChild(GetName(receiptInfo.PlayerId))
            wait()
            Sep(Items[receiptInfo.ProductId],p5)
        end
    end
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

game.Players.PlayerAdded:connect(function(p)
    if p~=nil then
        for i,v in pairs(Gamepasses) do
            if MarketplaceService:PlayerOwnsAsset(p,i) then
                Sep(v,p)
            end
        end
        wait(0.5)
        p.CharacterAdded:connect(function(p)--me changed c to p
            for i,v in pairs(Gamepasses) do
                if MarketplaceService:PlayerOwnsAsset(p,i) then
                    SepQ(v,p)
                end
            end
        end)
    end
end)

UPDATE edit:

So, I can fix the errors with this code but then my Gamepass only works locally and breaks in the server:

local function Sep(table,plr)
    for i,v in pairs(table, game.Players:GetPlayers()) do--me -removes stop working error after fixing clientside no Character, serverside no plr


        repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character     
        local char = plr.Character or plr.CharacterAdded:Wait()--me -removes Character a nil value error clientside
        plr = game.Players:GetPlayerFromCharacter(char)
        if type(v)=="userdata" then
            v:Clone().Parent = plr.Backpack
        elseif type(v)=="number" then
            game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name).Value = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name).Value + (v)
        else
            if v:sub(0,2) == "W:" then
                plr.Character.Humanoid.WalkSpeed = plr.Character.Humanoid.WalkSpeed + tonumber(v:sub(3))
            else
                --plr.Character.Humanoid.MaxHealth = v + 50
                --plr.Character.Humanoid.Health = v
                plr.Character.Humanoid.MaxHealth = plr.Character.Humanoid.MaxHealth + tonumber(v:sub(3))
                plr.Character.Humanoid.Health = plr.Character.Humanoid.Health
            end
        end
    end
end

How do I get this to run in both server and client and not break in the game? Also, perhaps unrelated is my test mode for server when I press F7 the player skin does not load and no rewards are given to player --- that is regardless of any errors in the Output

If I just leave this everything without trying to fix the error "attempt to index local 'plr' ( a nil value) everything works in the game just fine though should I just ignore the error ?

0
Put your code between the two ~ lines. hiimgoodpack 2009 — 7y

Answer this question