Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Player is nil in a function?

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

0

You can search via the Instance as well; you can simply pass the Player; tostring wouldn't work in this scenario.

0

also, use Player.Character instead of workspace[tostring(player)]

0

I fixed the indentation, and put the code in a code block.

0

I tried passing the player aswell, that's the problem.

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.