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

Player is nil in a function?

Asked by 5 years ago
Edited by TheeDeathCaster 5 years ago

So I got this annoying problem I've been trying to solve for more than an hour now.

Here's the code:

script:WaitForChild("DetectLevels").Event:connect(function()
    local IsInLight = false
    local function CheckForLight(Player)
        print(tostring(Player)) --Firstly prints my name and then prints "nil" a second time
        for i, l in pairs(game.Workspace:WaitForChild("Lights"):GetChildren()) do
            local distance = math.floor((game.Workspace:FindFirstChild( tostring(Player) ) .HumanoidRootPart.Position - l.Position).magnitude)
            if distance <= l:WaitForChild("Range").Value then
                --IsInLight = true
                return true
            end
            wait(.1)
        end
    end

    while wait(1) do
        for i, v in pairs(game:GetService("Players"):GetChildren()) do
            if game.Workspace:FindFirstChild(tostring(v)) then
                CheckForLight(tostring(v))
                print(tostring(v).." is in light: "..CheckForLight())
                wait(.1)
            end
        end
        wait(10)
    end
end)

I'd really really appreciate any sort of help!

0
You can search via the Instance as well; you can simply pass the Player; tostring wouldn't work in this scenario. Ziffixture 6913 — 5y
0
also, use Player.Character instead of workspace[tostring(player)] theking48989987 2147 — 5y
0
I fixed the indentation, and put the code in a code block. TheeDeathCaster 2368 — 5y
0
I tried passing the player aswell, that's the problem. wilsonsilva007 373 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Just found the issue.

The problem was with the print, doing: print( MyFunction() ) won't show the return value and just be nil, I'll be trying to solve that problem rn

Ad

Answer this question