Guns Not Randomizing

So this script is suppose to either give you a silenced smg or a baseball bat but it only gives the baseball bat even when i made the silenced smg chance higher, any solutions?

math.randomseed(tick())

local random = math.random(1,100)

function onClick(plr)
	script.Disabled = true	
	
	 if random >= 1 or random <= 50 then
		local item1 = game.ServerStorage.LootTable["Baseball Bat"] 
		item1:Clone().Parent = plr.Backpack

	elseif random >= 1 or random  <= 100 then
		local item2 = game.ServerStorage.LootTable["Silenced SMG"]
		item2:Clone().Parent = plr.Backpack
	end
	script.Parent.Parent.Parent.Parent:Destroy()
end

script.Parent.Triggered:Connect(onClick)

help would be nice, thanks!

1 Like

You call math.random once, meaning that random number will be the only one used

Put the local random = math.random(1,100) above the script.Disabled line in the onClick or anywhere before the if statement

Also, you don’t need to check random >= 1, the minimum number it can be one, you only really need to check using random <=

1 Like

Change the “or”'s to “and”'s. Also, move the random into the onClick function.

math.randomseed(tick())

function onClick(plr)
	local random = math.random(1,100)
	
	 if random >= 1 and random <= 50 then
		local item1 = game.ServerStorage.LootTable["Baseball Bat"] 
		item1:Clone().Parent = plr.Backpack
	elseif random > 50 then
		local item2 = game.ServerStorage.LootTable["Silenced SMG"]
		item2:Clone().Parent = plr.Backpack
	end
	
	script.Disabled = true
	script.Parent.Parent.Parent.Parent:Destroy()
end

script.Parent.Triggered:Connect(onClick)
1 Like

so with this script would there be a “rare” gun so like a 90 percent chance to get the baseball bat and a 10 percent for the silenced smg?

Just change the values.

math.randomseed(tick())

function onClick(plr)
	local random = math.random(1,100)
	
	 if random >= 1 and random <= 90 then
		local item1 = game.ServerStorage.LootTable["Baseball Bat"] 
		item1:Clone().Parent = plr.Backpack
	elseif random > 90 then
		local item2 = game.ServerStorage.LootTable["Silenced SMG"]
		item2:Clone().Parent = plr.Backpack
	end
	
	script.Disabled = true
	script.Parent.Parent.Parent.Parent:Destroy()
end

script.Parent.Triggered:Connect(onClick)

Also, in this case, you could really just use an else statement since there are only 2 items.

math.randomseed(tick())

function onClick(plr)
	local random = math.random(1,100)
	
	 if random >= 1 and random <= 90 then
		local item1 = game.ServerStorage.LootTable["Baseball Bat"] 
		item1:Clone().Parent = plr.Backpack
	else
		local item2 = game.ServerStorage.LootTable["Silenced SMG"]
		item2:Clone().Parent = plr.Backpack
	end
	
	script.Disabled = true
	script.Parent.Parent.Parent.Parent:Destroy()
end

script.Parent.Triggered:Connect(onClick)

but wouldnt that make the baseball bat and the silenced smg have the same rarity?

No, the baseball bat would come out if the number was between 1 and 90, otherwise, the smg would come out.

ooooh i see, lol sorry was a bit confused

would i be able to make more weapons be random or is it only 2?

Yes, you could make more weapons random ex:

math.randomseed(tick())

function onClick(plr)
	local random = math.random(1,100)
	
	 if random >= 1 and random <= 90 then
		local item1 = game.ServerStorage.LootTable["Baseball Bat"] 
		item1:Clone().Parent = plr.Backpack
	elseif random > 90 and random <= 99 then
		local item2 = game.ServerStorage.LootTable["Silenced SMG"]
		item2:Clone().Parent = plr.Backpack
	elseif random > 99 then
		local item3 = game.ServerStorage.LootTable["item"]
		item3:Clone().Parent = plr.Backpack
	end
	
	script.Disabled = true
	script.Parent.Parent.Parent.Parent:Destroy()
end

script.Parent.Triggered:Connect(onClick)

Also, you really don’t need to rename the variable each time they could all be called “item”, but to go with how you were doing it I named it “item3”

oh alright, thank you so much i really apperciate it

Also, I know that this is already solved, but you could make this much more simpler by doing something like this:

local items = {
	["Baseball Bat"] = {
		chance = 90,
		path = game.ServerStorage.LootTable["Baseball Bat"]
	},
	["Silenced SMG"] = {
		chance = 9,
		path = game.ServerStorage.LootTable["Silenced SMG"]
	},
	["Item"] = {
		chance = 1,
		path = game.ServerStorage.LootTable["Item"]
	},
}

local totalchance = 0

for i,v in pairs(items) do
	totalchance += v.chance
end

function onClick(plr)
	local random = math.random(1, totalchance)
	local currentchance = 0
	
	for i,v in pairs(items) do
		currentchance += v.chance
		if random <= currentchance then
			v.path:Clone().Parent = plr.Backpack
			break
		end
	end
	
	script.Disabled = true
	script.Parent.Parent.Parent.Parent:Destroy()
end
1 Like

i tried it and it didnt work lol, thank you for showing me this though