Attempt to compare nil < number

Hey Developers,

I made a script that spawns a “Dough” part but it doesn’t work and in the output says the following:

Workspace.DoughSpawner.Script:7: attempt to compare nil < number - Server - Script:7
10:28:44.329 Stack Begin - Studio
10:28:44.329 Script ‘Workspace.DoughSpawner.Script’, Line 7 - Studio - Script:7
10:28:44.329 Stack End - Studio

Can you help me?

This is my script:

	
local spawner = script.Parent

while task.wait(2) do
	local spawned = spawner:GetAttribute("DoughPart")
	if spawned < 5 then
		local dough = rs.DoughPart:Clone()
		dough.Parent = game.Workspace
		dough.Position = spawner.Position
		spawner:SetAttribute("DoughSpawned", spawner:GetAttribute("DoughSpawned") + 1)
	end
end
1 Like

Does the attribute exist? Did you spell it correctly? Is the attribute a number? Does it have anything on it?

1 Like

Change “DoughPart” to “DoughSpawned”.

Try this:

local spawner = script.Parent

while task.wait(2) do
	local spawned = spawner:GetAttribute("DoughSpawned")
	if spawned < 5 then
		local dough = rs.DoughPart:Clone()
		dough.Parent = game.Workspace
		dough.Position = spawner.Position
		spawner:SetAttribute("DoughSpawned", spawner:GetAttribute("DoughSpawned") + 1)
	end
end

bro you sent me the same script

I doesn’t work its not about the Dough part

I made a mistake I didn’t copy the first command

this is the original script:

local rs = game:GetService("ReplicatedStorage")

local spawner = script.Parent

while task.wait(2) do
	local spawned = spawner:GetAttribute("DoughSpawned")
	if spawned < 5 then
		local dough = rs.DoughPart:Clone()
		dough.Parent = game.Workspace
		dough.Position = spawner.Position
		spawner:SetAttribute("DoughSpawned", spawner:GetAttribute("DoughSpawned") + 1)
	end
end

Could you add print like so and see where and even if the value ever changes?

print(spawned .. "1")
while task.wait(2) do
    print(spawned .. "2")
	local spawned = spawner:GetAttribute("DoughSpawned")
    print(spawned .. "3")
	if spawned < 5 then

I changed 1 thing, but ok. Also error means your Attribute named wrong.

what do you mean can you tell me how do i need to call it?

Can you show me attribute in properties?

you mean the spawner or the doughpart?

Spawner

which category I must send??? part category?

Attributes

The is nothing there what do I have to add?

Then show me attributes of doughpart.

also nothing there what do i have to add?

Checking for nil


Always check your values/attributes for possible nil values, and replace them with the default values.

In your case, I can assume another script might add the attribute later on. This check will assume it’s already a value so you might just want to add a check to ensure that it exists before comparing.

local spawner = script.Parent

while task.wait(2) do
	local spawned = spawner:GetAttribute("DoughSpawned")
    if not spawned then spawned = 0; spawner:SetAttribute("DoughSpawned", 0) end --| Ensures its not nil and replaces with 0

	if spawned and spawned < 5 then
		local dough = rs.DoughPart:Clone()
		dough.Parent = game.Workspace
		dough.Position = spawner.Position
		spawner:SetAttribute("DoughSpawned", spawner:GetAttribute("DoughSpawned") + 1)
	end
end

Add attribute to spawner and make it number attribute and name it DoughSpawned. Then test script.