Gun that's supposed to shoot random bullets is not working

(I reposted this because nobody saw the previous post) I was trying to make a gun that shoots random bullets, but everytime I try to make it shoot random bullets, it either shoots only one everytime, or it just doesn’t shoot at all. I’m using a script someone else made and modifying it to turn it into what I want it to be

script

script.Parent.OnServerEvent:Connect(function(plr)
	local folder = game.ReplicatedStorage.MK2Ammo
	
	local Red = folder.Red
	
	local Orange = folder.Orange
	
	local Yellow = folder.Yellow
	
	local Green = folder.Green
	
	local Blue = folder.Blue
	
	local Purple = folder.Purple
	
	local Pink = folder.Pink
	
	local selecteditem = {Red, Yellow, Orange, Green, Blue, Purple, Pink}
	local Slash = game.ReplicatedStorage.MK2Ammo.selecteditem.OuterBullet:Clone()--change the attack name
	local Slash2 = game.ReplicatedStorage.MK2Ammo.selecteditem.InnerBullet:Clone()--change the attack name
	
	
	
	Slash.Parent = workspace
	Slash.CanCollide = false
	Slash.Anchored = false
	Slash.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(2,0,2) * CFrame.fromEulerAnglesXYZ(5,0,5)
	Slash2.Parent = workspace
	Slash2.CanCollide = false
	Slash2.Anchored = false
	Slash2.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(2,0,2) * CFrame.fromEulerAnglesXYZ(5,0,5)
	local Weld = Instance.new("WeldConstraint")
	Weld.Parent = Slash
	Weld.Part0 = Slash
	Weld.Part1 = Slash2
	Slash.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
			local Explosion = game.ReplicatedStorage.LightingPew.Explosion:Clone()--change folder name
			Explosion.Parent = workspace
			Explosion.CFrame = Slash.CFrame
			Explosion.CanCollide = false
			Explosion.Anchored = true
			Slash:Destroy()
			hit.Parent.Humanoid:TakeDamage(15)	--Change damage
			for i = 1,18 do
				wait(.05)
				Explosion.Size = Explosion.Size + Vector3.new(2,2,2)
				Explosion.Transparency = Explosion.Transparency + 1
			end	
			Explosion:Destroy()	
		end
	end)
	local BV = Instance.new("BodyVelocity",Slash)
	BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	BV.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 100
end)

Please tell me what’s the problem

The table selecteditem contains all the bullets. You need to select a bullet at random from that table to use - see math.random.

but everytime I tried to use math.random, it came with an error saying “selected item isn’t a child of MKAmmo” or something. I’m most likely using it wrong

Then there is “selected item”. Or it isn’t a child of MKAmmo.

local folder = game.ReplicatedStorage.MK2Ammo:GetChildren()

local randombullet = folder[math.random(#folder)]

@hjfghhgglosthisuser The randombullet variable works.

We can reference #folder since the default min value is 1 so we can just leave that blank.

local items = {Red, Yellow and so on} 
local itemNumber = math.random(1, #items)
local selectedItem = items[itemNumber]

it comes with the same error though

it shows the same error again though, but the explanation kinda cleared things up

Can you copy and paste (or screenshot) the error?

Could you try again, I about 99% sure that this works if you are using it correctly.

Just tested it, my script works.

local folder = game.ReplicatedStorage.MK2Ammo:GetChildren()

local randombullet = folder[math.random(#folder)]

Try testing it again. If the problem continues, send a picture of the screenshot.

this

@regibus361’s script does not work. Please try mine like I have been asking you.

this. I’m using it wrong aren’t I?

where is your bullet located and what’s it called?

On the lines beginning local Slash = and local Slash2 =, just use randombullet.OuterBullet:Clone() instead of game.ReplicatedStorage.randombullet. I assume that’s the cause anyway

Send the full script, it is working for me.

script.Parent.OnServerEvent:Connect(function(plr)
	local folder = game.ReplicatedStorage.MK2Ammo
	
	local Red = folder.Red
	
	local Orange = folder.Orange
	
	local Yellow = folder.Yellow
	
	local Green = folder.Green
	
	local Blue = folder.Blue
	
	local Purple = folder.Purple
	
	local Pink = folder.Pink
	
	local folder = game.ReplicatedStorage.MK2Ammo:GetChildren()

local randombullet = folder[math.random(#folder)]
	
	
	local Slash = game.ReplicatedStorage.MK2Ammo.randombullet.OuterBullet:Clone()--change the attack name
	local Slash2 = game.ReplicatedStorage.MK2Ammo.randombullet.InnerBullet:Clone()--change the attack name
	
	
	
	Slash.Parent = workspace
	Slash.CanCollide = false
	Slash.Anchored = false
	Slash.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(2,0,2) * CFrame.fromEulerAnglesXYZ(5,0,5)
	Slash2.Parent = workspace
	Slash2.CanCollide = false
	Slash2.Anchored = false
	Slash2.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(2,0,2) * CFrame.fromEulerAnglesXYZ(5,0,5)
	local Weld = Instance.new("WeldConstraint")
	Weld.Parent = Slash
	Weld.Part0 = Slash
	Weld.Part1 = Slash2
	Slash.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name then
			local Explosion = game.ReplicatedStorage.LightingPew.Explosion:Clone()--change folder name
			Explosion.Parent = workspace
			Explosion.CFrame = Slash.CFrame
			Explosion.CanCollide = false
			Explosion.Anchored = true
			Slash:Destroy()
			hit.Parent.Humanoid:TakeDamage(15)	--Change damage
			for i = 1,18 do
				wait(.05)
				Explosion.Size = Explosion.Size + Vector3.new(2,2,2)
				Explosion.Transparency = Explosion.Transparency + 1
			end	
			Explosion:Destroy()	
		end
	end)
	local BV = Instance.new("BodyVelocity",Slash)
	BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	BV.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 100
end)

Remove the ‘game.ReplicatedStorage.MK2Ammo.’ (and do the same on the next line)

isn’t it math.random(int, int) and not math.random(int)