What value is nil?

Okay, this ones driving me a little insane. I’ll have to tell you how my system works so you can truly understand what my problem is.

So in the current state of my game, a player spawns in a lobby, it waits until a round starts. When a round starts, there will be a 5 second countdown. Starting from that countdown, the player can deploy into the map, but said player will be frozen and their weapon will be disabled until the countdown ends.

“Alright, cool! Ill just make a BoolValue that makes it so the weapon doesnt activate when the value is false.” Is what i thought. I made it, and yes, it worked exactly as intended… only without the round system.

With the round system, for some reason, whenever i deploy after the countdown, this error pops up:

image

For context, Line 54 of “Main” script is an event that fires when the player presses the Deploy button, it fires a function that is stored in a ModuleScript, which is where the error lies. This is what Line 136 says:

char:FindFirstChildOfClass("Tool"):FindFirstChild("Enabled").Value = true

Let me give you a bigger picture:

if stationizeplayer then -- this value is turned on during the countdown and off after
	spawn(function()
		while stationizeplayer do
			char.Humanoid.WalkSpeed = 0
			wait()
		end
		if char:FindFirstChildOfClass("Tool") and char:FindFirstChildOfClass("Tool"):FindFirstChild("Stats") then
			local module = require(char:FindFirstChildOfClass("Tool").Stats)
			char.Humanoid.WalkSpeed = module.Speed
			char:FindFirstChildOfClass("Tool"):FindFirstChild("Enabled").Value = true
		end
	end)
else
	char:FindFirstChildOfClass("Tool"):FindFirstChild("Enabled").Value = true -- the error line, not the one above
end

In the game, all the weapons “Enabled” value is set to false by default, so i have to turn it on everytime a player fairly equips the weapon. I can assure you, every weapon in the folder has this value.

I think ive told everything that’s necessary. Im sorry if my tone was a bit off in this post. Any help will be highly appreciated.

Hello, I see the error code is part cut out, can you provide the full error code?

Perhaps try this:

for _,v in pairs(char:GetChildren()) do
if v:IsA(“Tool”) then
v.Enabled.Value = true
end
end

Can you please give the line where the error is in? (line 136)

You shouldn’t chain :FindFirstChild like that, it’s only really used for checking if something exists before indexing. If you’re not checking, you should default to dot indexing. From what it looks like, either char is nil or something in the chain is.

2 Likes

Are you sure the player will always have the tool equipped when this code is executed?

Assuming that the part of the line char:FindFirstChildOfClass("Tool") returns nil, :FindFirstChild("Enabled") would return nil and .Value would give this error.

If the player did not have the tool equipped, it would not be in the character, (rather, it would be in the player’s backpack) and that line would error.

1 Like

For everytime the player deploys, it will be given a weapon in this order:

  1. the (cloned) weapon is parented to the character
  2. char.Humanoid:EquipTool(weapon)

and the rest of the code runs up to the error line

Youre right with the FindFirstChild thing, but the problem is, even if in the situation where “stationizeplayer” is true, the player spawns perfectly fine, even if the same exact line of code is placed there.

Use WaitForChild instead of FindFirstChild if you want to wait for Enabled to be there. It may be that you’re checking too soon.

If your code hangs when you make this change, then Enabled never gets parented to the tool or has already been destroyed.

Also the main difference between the two blocks is that in the one that works, you check if tool exists before then trying to index its children. For the one that errors, you don’t bother checking. If you’d like to find out why one works and the other doesn’t, it’s probably worth setting up the same checks for both blocks.

I have wrote this quick, perhaps it could work.

CODE:

if stationizeplayer then
     spawn(function()
          while stationizeplayer do
               if char and char.Humanoid then
                    char.Humanoid.WalkSpeed = 0;
                    wait()
               end;
          end;
          if char:FindFirstChildOfClass("Tool") and char:FindFirstChildOfClass("Tool"):FindFirstChild("Stats") then
               local Tool = char:FindFirstChildOfClass("Tool");
               local Stat = Tool:WaitForChild("Stats");
               local Module = require(Stats)
               char.Humanoid.WalkSpeed = Module.Speed;
               Tool:WaitForChild("Enabled").Value = true;
          end;
     end);
elseif char:FindFirstChildOfClass("Tool") then
     char:FindFirstChildOfClass("Tool"):WaitForChild("Enabled").Value = true;
end;

The issue might be that Tool has a property called ‘Enabled’, and you’re accessing that instead of the instance boolvalue. Try renaming your BoolValues or see if you can remove them and work with the ‘Enabled’ property. Tool | Documentation - Roblox Creator Hub

@20amir02 My best bet here is that the user is not equipping any tool when the code runs, hence why it says you’re indexing a nil value. There simply is no tool in the Character.
Here’s a really simple quick fix for that line:

local Tool = char:FindFirstChildOfClass("Tool")

if Tool then Tool.Enabled.Value = true end

This is not the issue, as OP is using FindFirstChild. The function returns the first child on the Instance that has the specified name, regardless of whether or not there is any property with that name.

2 Likes