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

I need help with my random drop chance?

I'm drying to make a random drop chance where theres only a certain amount of chance for the enemy to drop the weapon but so far it isnt going to well, please help me

local Humanoid = script.Parent.Enemy -- Change Enemy to the name of your Monsters Humanoid

g = math.random(1,5)
if g == 2 then
function Drop() 
local tag = Humanoid:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
local bp = tag.Value:findFirstChild("Backpack") 
local sp = tag.Value:findFirstChild("StarterGear")
			if bp ~= nil and sp~=nil and sp:findFirstChild("GrassMace")==nil then 
wep = game.Lighting.GrassMace:Clone()
wep.Parent = bp
wep2 = wep:Clone()
wep2.Parent = sp
wait(0.1)
script:remove()
			end 
		end 
	end 
end 
Humanoid.Died:connect(Drop) 

2 comments

1

put the random number and the if statement inside of the function instead of outside. Because currently line 22 is looking for a function that may not exist.

0

@SchonXIV, it says "Monsters" not "Players" in line 1... Something to think about.

1 answer

✓ Accepted answer

There's a few things that are wrong with this script so let's start from scratch

First let's set the monster's variable

local monster = workspace.Monster

Next let's set up a died event

monster:WaitForChild("Humanoid").Died:connect(function() --waitforchild to check if it's there

end)

Next getting the player who killed him, Most weapons have a tagging script built into them to record kills/deaths for character stats. You can utilize this feature to find the killer. usually the tag's value is the player. But if your weapon doesn't have this feature then you'll have to create it in your weapon.

monster:WaitForChild("Humanoid").Died:connect(function() --waitforchild to check if it's there
	local tag = monster.Humanoid:findFirstChild("creator")
	if tag then

	end
end)

Next let's set up our chance

monster:WaitForChild("Humanoid").Died:connect(function() --waitforchild to check if it's there
	local tag = monster.Humanoid:findFirstChild("creator")
	if tag and tag.Value then
		local chance=math.random(1,5)
		if chance==2 then

		end
	end
end)

And now give the player the weapons if he doesn't have them

monster:WaitForChild("Humanoid").Died:connect(function() --waitforchild to check if it's there
	local tag = monster.Humanoid:findFirstChild("creator")
	if tag and tag.Value then
		local chance=math.random(1,5)
		if chance==2 then
			if tag.Value.Backpack:FindFirstChild("GrassMace")==nil then
				local wep = game.Lighting.GrassMace:Clone()
				wep.Parent=tag.Value.Backpack
			end
		end
	end
end)

No need to Destroy the script if the script is located inside the monster because it will be removed with the rest of the model and we don't need to clone the weapon to starterpack

Final Form:

local monster = workspace.Monster

monster:WaitForChild("Humanoid").Died:connect(function() --waitforchild to check if it's there
	local tag = monster.Humanoid:findFirstChild("creator")
	if tag and tag.Value then
		local chance=math.random(1,5)
		if chance==2 then
			if tag.Value.Backpack:FindFirstChild("GrassMace")==nil then
				local wep = game.Lighting.GrassMace:Clone()
				wep.Parent=tag.Value.Backpack
			end
		end
	end
end)

1 comment

0

thankyou very much

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