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)
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)