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
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
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
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