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
Hello.
Issues:
You attempted to connect your function inside the function you were trying to connect.
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.
You'd need a coroutine so the script wouldn't get stuck in the while count == 1000
loop.
Improvments:
Use DataModel:GetService() instead of using the conditional dot operator.
Use local
before indexing your function.
Set the mp's position before parenting it.
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!
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
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)