Can't find character in Workspace? Script gives error

local function createBeam(plr, toPoint)
	-- plr = player
	-- toPoint = Where the arrow will go to.

	-- Find player in workspace
	print(plr, type(plr.Name))
	if game.Workspace:FindFirstChild(plr.Name) then
        -- If statement returns error "Attempt to index nil with 'Name'"
	end
end

wait(5)
print(game.Players.Voctal.Name)
wait(5)
createBeam(game.Players.Voctal, Vector3.new(3, 3, 3))

errorr
By the way, Voctal is me, I’m just using an alt to test this.

So this script worked fine a moment ago but now it stopped working and gives me this error… I have no clue why because it shouldn’t error. Workspace has my character and plr has a .Name property.

EDIT SOLVED: I accidentally called renamed the main function and then somehow had the content call itself from within with no function parameters… so yeah that’s what caused the problem.

The function seems to be running multiple times.

image

Check out those line numbers. Same one.

I removed the other function call and it still errors. Running it N amount of times wouldn’t make a difference. The problem is with the if statement. :FindFirstChild for some reason doesn’t find “Voctal” in Workspace and I have no clue why because it exists there.

That error is not related to the if statement, so no the problem is not related to the if statement. Lua just doesn’t magically break and say “plr” is nil when it wasn’t above it.

Regardless, don’t use “workspace.PlayerName” as a way to get the character. Use

local character = plr.Character
1 Like

Regarding what you wrote in the OP here, I think you’re misunderstanding the error. It’s not complaining about the character being nil or the .Name being nil. It’s saying “plr” is nil in one of the function calls - as in the variable is undefined.

1 Like

The error means that plr does not have a .Name property. The print above it shows that plr.Name is a string and that it does exist; it’s “Voctal”. Now, just like finding parts in Workspace with FindFirstChild, why am I unable to do this now when this literally worked earlier. Remember, this script worked just fine earlier.

Sending game.Players.Voctal.Name is sending the string Votcal, you can call .Name on a string. Consider sending game.Players.Votcal instead. Have you checked what the name was in the workspace? Maybe it changed because of another script.

I’ve tried that, still doesn’t work because that’s an instance.

No, I’m sorry but your script might be confusing you. I’m not sure why you’re stuck on the idea it magically broke. Please provide more output and all of the places you are firing this function. I’ve done this for over 15 years and I can assure you there is something that is tricking us here about this error. As I said, you are reading the error wrong - it is saying “plr” is nil.

Please show the full output and split up your print statement (otherwise you won’t see it printing nil). Such as:

print(plr)
print(type(plr.Name))

Your error is happening because you sent both arguments to print - and it isn’t showing you that “plr” is actually nil.

1 Like

My bad. I was calling the function from within the function with no function parameters. What a silly mistake. Or rather, I had accidentally renamed the main function to the other functions name…

1 Like