Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Function not looping because of a nil value?

Asked by 4 years ago

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
0
the error is that in output it says attempt to call a nil value and the script isnt looping coolmanHDMI 72 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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!

0
@coolmanHDMI this works this is best answer ayuu_ondev 60 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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
0
Does this work @coolmanHDMI ayuu_ondev 60 — 4y
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)

Answer this question