How can I do this simpler?

Hello Forum!

I’m making a crate system, and I don’t know how to make this code “simpler.” Basically, I am detecting" if the random number equals # then" for 7 numbers meaning 7 checks which makes the code look really messy. How can I shorten this and make it cleaner? All help is appreciated. Thanks!

local Crate1 = game.Workspace.Crate1Trigger.ProximityPrompt

Crate1.Triggered:Connect(function(plr)
	
	local cratePrice = 100
	local LS = plr:FindFirstChild("leaderstats")
	local Objects = game.ReplicatedStorage.Crate1
	
	if LS.Coins.Value < cratePrice then
		return
	end
	
	LS.Coins.Value = LS.Coins.Value - 100
	
	local Items = {
		
		knife1 = Objects.Knife1;
		knife2 = Objects.Knife2;
		knife3 = Objects.Knife3;
		knife4 = Objects.Knife4;
		knife5 = Objects.Knife5;
		knife6 = Objects.Knife6;
		knife7 = Objects.Knife7;
		
	}
	
	Objects.Crate1Bought:FireClient()
	
	local randomNum = math.random(1, 7)
	
	if randomNum == 1 then
		-- blah blah
	end
	if randomNum == 2 then
		-- blah blah
	end
	if randomNum == 3 then
		-- blah blah
	end
	if randomNum == 4 then
		-- blah blah
	end
	if randomNum == 5 then
		-- blah blah
	end
	if randomNum == 6 then
		-- blah blah
	end
	if randomNum == 7 then
		-- blah blah
	end
end)

All help is appreciated!

If I understand well, you trying to choose one of those knifes randomly ?
you can do something like this maybe

local Items = {
		
		knife1 = Objects.Knife1;
		knife2 = Objects.Knife2;
		knife3 = Objects.Knife3;
		knife4 = Objects.Knife4;
		knife5 = Objects.Knife5;
		knife6 = Objects.Knife6;
		knife7 = Objects.Knife7;
		
	}

local knife = Items["knife"..math.random(1, 7)]

you can also remove indexes and use “#” so you can add any amount of knifes

local Items = {
		
		Objects.Knife1;
		Objects.Knife2;
		Objects.Knife3;
		Objects.Knife4;
		Objects.Knife5;
		Objects.Knife6;
		Objects.Knife7;
		
	}

local knife = Items[math.random(1, #Items)]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.