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!

4 comments
You can search via the Instance as well; you can simply pass the Player; tostring wouldn't work in this scenario.
also, use Player.Character instead of workspace[tostring(player)]
I fixed the indentation, and put the code in a code block.
I tried passing the player aswell, that's the problem.