Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Function not looping because of a nil value?

I need this loop to work so that he drops the block multiple times but it doesnt loop and says "attempt to call a nil value" pls help

function bloc()
	local count = 1000
	while count == 1000 do	
	local gb = script.Parent.Health
	local mp = game.Lighting.money
	if gb <= 29999 then
		wait(2)
		mp:Clone()
		mp.Parent = game.Workspace
		mp.Position = script.Parent.Parent.HumanoidRootPart.Position
	end
    end
script.Parent.HealthChanged:Connect(bloc)
end

1 comment

0

the error is that in output it says attempt to call a nil value and the script isnt looping

3 answers

✓ Accepted answer

Hello.

Issues:

  1. You attempted to connect your function inside the function you were trying to connect.

  2. You didn't create a variable for the cloned mp, so you'd be changing the parent of the money in Lighting, not the clone of it.

  3. You'd need a coroutine so the script wouldn't get stuck in the while count == 1000 loop.

Improvments:

  1. Use DataModel:GetService() instead of using the conditional dot operator.

  2. Use local before indexing your function.

  3. Set the mp's position before parenting it.

  4. Name the function more correctly so you don't get confused on what function is what.

Fixed code:

local function onHealthChanged()
	local count = 1000

	coroutine.wrap(function()
		while count == 1000 do  
			local gb = script.Parent.Health
			local mp = game:GetService("Lighting").money
	
			if gb <= 29999 then
				wait(2)

				local clonedMp = mp:Clone()
				clonedMp.Position = script.Parent.Parent.HumanoidRootPart.Position
				clonedMp.Parent = workspace
			end
		end
	end)()
end

script.Parent.HealthChanged:Connect(onHealthChanged)

Please accept and upvote this answer if it helped!

1 comment

0

@coolmanHDMI this works this is best answer

One thing I see is that you called your function in the function you wanted to call the proper script is

function bloc()
    local count = 1000
    while count == 1000 do  
    local gb = script.Parent.Health
    local mp = game.Lighting.money
    if gb <= 29999 then
        wait(2)
        mp:Clone()
        mp.Parent = game.Workspace
        mp.Position = script.Parent.Parent.HumanoidRootPart.Position
    end
    end

end
while true do
	script.Parent.HealthChanged:Connect(bloc)
end

1 comment

0

Does this work @coolmanHDMI

Try this:

while (count) == 1000 do
local function bloc()
    local count = 1000
    local gb = script.Parent.Health
    local mp = game.Lighting.money
    if gb <= 29999 then
coroutine.wrap(function()
        wait(2)
        mp:Clone()
        mp.Parent = game.Workspace
        mp.Position = script.Parent.Parent.HumanoidRootPart.Position
    end
    end

end) ()
end
script.Parent.HealthChanged:Connect(bloc)

This discussion is preserved as it was. New answers, comments, and votes are closed.