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

trying to fix argument 1 missing or nil? to make object spawn in a 1 out of 150 chance

i keep getting this message on this script im working on. I'm still pretty new to scripting so im not sure exactly how to fix this.

function Drop() 
	
local drops = {"NUKE"} 
local G = math.random(1, 150) 
local Obj = game.Lighting:findFirstChild(drops[G]):Clone() 
	
Obj.Parent = game.Workspace 
Obj.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(4,-3,0) 
script:remove()
end 


while true do 
	wait(.1) 
	if (script.Parent.Humanoid.Health <1) then 
		Drop() 
	end 
end

basically i want it so if the math.random number lands on "1" then the nuke will spawn, and if it lands on 2-150, then nothing will spawn.

now the item does spawn in, but i keep getting this error code in output and i'd like to clean up everything i can

2 answers

✓ Accepted answer

The above answer is almost right, But you're not checking to see if the selected table entry is nil or not.

Here's my $5 bucks at a stab at this ....

My code:

function Drop() 

	local drops = {"NUKE"} 
	local G = math.random(1, 150) 
	local Selected = drops[G] -- make this a variable, so we can check it in the next step
	if(Selected ~= nil) then -- if its not nil then continue otherwise skip!
		local Obj = game.Lighting:findFirstChild(Selected):Clone() 
		if(Obj ~= nil) then -- make sure theres one available to clone (Might break on the line above)
			Obj.Parent = game.Workspace 
			Obj.Handle.CFrame = script.Parent.Head.CFrame + Vector3.new(4,-3,0)
	--	else
	--		warn("Warning: NUKE is nil!")
		end
	--else
	--	print("Selected[",G,"] = nothing!")
	end
	script:remove()
end 


while true do 
	wait(.1) 
	if (script.Parent.Humanoid.Health <1) then 
		Drop() 
	end 
end

seems to run but rarely lands on the nuke, kinda what you wanted

Hope this helps!

You're problem is here:

local drops = {"NUKE"} 
local G = math.random(1, 150) 
local Obj = game.Lighting:FindFirstChild(drops[G]):Clone()

Currently you try to get a random object, however you only have one object. If it decides to try and grab 132 item in the table, however you do not have 132 or more objects in the table, it'll return nil.

So, what you wanna do is use math.random in a sense of how many you have in the table.

local Drops = {'NUKE'}
local G = math.random(1,#Drops) -- gets between 1 and the amount of drops
if G ~= nil then
	local Obj = game.Lighting:FindFirstChild(Drops[G]:Clone()
end -- check if drops actually has items

Edited version for what you want from the new comment:

local Drops = {'NUKE'}
local G = math.random(1,150) -- gets number between 1 and 150
if G == 1
	local Obj = game.Lighting:FindFirstChild(Drops[1])
	if Obj then
		Obj:Clone().Parent = nil -- change this to your parent
	end -- check for drop
else
	-- not 1, try something else?
end -- check if drops actually has items

3 comments

0

i tried this but got the error code " : Expected ')' (to close '(' at line 6), got 'end' "

0

ok i may have explained this poorly, what i want to happen is in the math.random i want it so if the number lands on "1" then the nuke will drop. any if it lands on any numbers between 2 - 150 then nothing will happen

0

Ah, let me fix this accordingly.

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