Variable is nil when finding something in the player's character

Hello!

  • LureName is working
  • ToolEquipped is nil

Locatinig LureName section of code:

CharChildAdd.OnServerEvent:Connect(function(plr, NameValue, CostValue, MinigameSpeedValue, LuckValue, RummageAmountValue)

	LureName = NameValue
	LureCost = CostValue
	LureMinigameSpeed = MinigameSpeedValue
	LureLuck = LuckValue
	LureRummageAmount = RummageAmountValue

	print(LureName)
	print(LureLuck)

ToolEquipped Variable section of code:

local toolEquipped = workspace[plr.Name]:WaitForChild(LureName)
	print(toolEquipped.Name)
	if toolEquipped then

toolEquipped is nil, is there anything wrong with the code?

I placed breakpoints and LureName in the toolEquipped part is working but toolEquipped is nil.

When the player leaves and rejoins it works fine but when the player first gets the lure it toolEquipped is nil.

Any solutions?

When this bug happens, the player doesn’t start with the lure(which is a tool) they purchase it and then when they use it toolEquipped is nil.

Well, first off you’re using WaitForChild(). So, if the instance doesn’t exist, it will infinitely yield until it is found (which might be never)

So, you should change to :FindFirstChild(). This will check once, and if there is no LureName in the character, then it will return nil.

  • Alternatively, you could specify a time argument in :WaitForChild(), which is done by adding :WaitForChild(object, time). This will terminate the process after that many seconds, although there’s no reason to use this in most cases and :FindFirstChild is better here.

toolEquipped is nil.

Also I got this error:
11:55:17.363 Workspace.OminusOfficesMap.Dumpster1.DumpsterOpenMain.Main.WeightedItemDistribution:103: attempt to index nil with ‘Name’ - Server - WeightedItemDistribution:103

local toolEquipped = workspace[plr.Name]:WaitForChild(LureName)
	print(toolEquipped.Name)
	if toolEquipped then

No the LureName is not the player’s name it is a tool name.

I just removed the comment, before you replied. :rofl:

1 Like

Do you know a reason why toolEquipped is nil?

Maybe this… Think that is like findfirstchild() but even more directly. If that isn’t loaded yet = nil.
local toolEquipped = workspace:WaitForChild(plr.Name):WaitForChild(LureName)

It says its infinite yeilding on the LureName which is wierd because it is in the player’s charachter.

That’s good. it means that may have an error and now we’re on the the next error.

You had: (true or nil):(wait for true) --probably the reason for the nil
Now you have (wait for true):(wait for true)
And now you’re seeing the time limit error from one of them waits.
That means you’re not setting that to where it is exactly…

So what do i do?


When I checked the explorer:

  • In the client side I can see the tool in the player’s charachter
  • In the server side I can’t see the tool
  • The script is a serverscript meaning it doesn’t see the tool.

What would I need to do so it does see the tool on the server side.

local toolEquipped = workspace:WaitForChild(plr.Name):WaitForChild(LureName, 5)

I’m assuming you have waited for everything involved to be in place and have print checked them.

Wait, this is probably wrong but, isn’t the player character called the player display name? Like if your roblox name is 2973udj and your display name is yyear22 the character is called yyear22.