Crate System Issue

I made a crate system, but there’s one issue. 90% of the images/frames dont load at all, and are just the placeholder. I assume its due to poor optimization. Help?

plr = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function()
	if plr.leaderstats.CommonCrates.Value > 0 then
		plr.leaderstats.CommonCrates.Value -= 1
		plr.PlayerGui.CommonCrate.Frame.Visible = true
		script.Parent.Parent.Parent.Parent.Parent.Parent.Enabled = false
		local win = nil
		for i = 1, 25 do
			local random1 = math.random(1,100)
			local clo = game.ServerStorage.Template:Clone()
			clo.Parent = plr.PlayerGui.CommonCrate.Frame.Frame.hd
			if NumberRange.new(1,70).Min < random1 and random1 < NumberRange.new(1,70).Max or NumberRange.new(1,70).Min == random1 or NumberRange.new(1,70).Max == random1 then
				local random = math.random(1,7)
				for i, v in pairs(game.ServerStorage.Skins.Common:GetChildren()) do
					if v:GetAttribute("rank") == random then
						local clo2 = v:Clone()
						clo2.Parent = clo.ViewportFrame.WorldModel
						clo2.Handle.Position = Vector3.new(0,0,0)
						clo.Name = clo2.Name
						clo.BackgroundColor3 = Color3.new(0.75,0.75,0.75)
						clo.TextLabel.Text = "COMMON"
						clo.TextLabel.BackgroundColor3 = Color3.new(0.6,0.6,0.6)
					end
				end
			end
			if NumberRange.new(70,95).Min < random1 and random1 < NumberRange.new(70,95).Max or NumberRange.new(70,95).Min == random1 or NumberRange.new(70,95).Max == random1 then
				local random = math.random(1,6)
				for i, v in pairs(game.ServerStorage.Skins.Uncommon:GetChildren()) do
					if v:GetAttribute("rank") == random then
						local clo2 = v:Clone()
						clo2.Parent = clo.ViewportFrame.WorldModel
						clo2.Handle.Position = Vector3.new(0,0,0)
						clo.Name = clo2.Name
						clo.BackgroundColor3 = Color3.new(0,0.75,0)
						clo.TextLabel.Text = "UNCOMMON"
						clo.TextLabel.BackgroundColor3 = Color3.new(0,0.6,0)
					end
				end
			end
			if NumberRange.new(95,100).Min < random1 and random1 < NumberRange.new(95,100).Max or NumberRange.new(95,100).Min == random1 or NumberRange.new(95,100).Max == random1 then
				local random = math.random(1,5)
				for i, v in pairs(game.ServerStorage.Skins.Rare:GetChildren()) do
					if v:GetAttribute("rank") == random then
						local clo2 = v:Clone()
						clo2.Parent = clo.ViewportFrame.WorldModel
						clo2.Handle.Position = Vector3.new(0,0,0)
						clo.Name = clo2.Name
						clo.BackgroundColor3 = Color3.new(0.75,0,0)
						clo.TextLabel.Text = "RARE"
						clo.TextLabel.BackgroundColor3 = Color3.new(0.6,0,0)
					end
				end
			end
			if i == 19  then
				win = clo
				print("winna")
			end
		end
		plr.PlayerGui.CommonCrate.Frame.Frame.hd:TweenPosition(UDim2.new(-4,0,0,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quart,8)
		task.wait(9)
		plr.PlayerGui.CommonCrate.Frame.Frame.hd.Position = UDim2.new(0,0,0,0)
		local ski = Instance.new("Folder")
		ski.Name = win.Name
		ski.Parent = plr.Skins
		plr.PlayerGui.CommonCrate.Frame.Visible = false
		script.Parent.Parent.Parent.Parent.Parent.Parent.Enabled = true
		for i, v in pairs(plr.PlayerGui.CommonCrate.Frame.Frame.hd:GetChildren()) do
			if v:IsA("Frame") then
				v:Destroy()
			end
		end
	end
end)
3 Likes

Is this a localscript or a serverscript, this is too much! Please make the code more easier to see.

2 Likes

Sorry about the length, its a serverscript. Ill try to optimize and add indents.

Ok, so I make it so it shows the frames 90% of the time now by just trying to make it easier to read, rendering my post redundant. You saying “make it easier to see” actually make me optimize my code lol, here is the cleaned up version incase you wanna see it:

--variables
plr = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
commonrange = NumberRange.new(1,10)
uncommonrange = NumberRange.new(10,55)
rarerange = NumberRange.new(55,90)
legendaryrange = NumberRange.new(90,100)
--the setting function
local function set(tex,bgcolor,textbgcolor,v,clo)
	local clo2 = v:Clone()
	clo2.Parent = clo.ViewportFrame.WorldModel
	clo2.Handle.Position = Vector3.new(0,0,0)
	clo.Name = clo2.Name
	clo.BackgroundColor3 = bgcolor
	clo.TextLabel.Text = tex
	clo.TextLabel.BackgroundColor3 = textbgcolor
end
rew = {
	common = {
		game.ServerStorage.Skins.Common["BlackSword"],
		game.ServerStorage.Skins.Common["BlueSword"],
		game.ServerStorage.Skins.Common["GreenSword"],
		game.ServerStorage.Skins.Common["OrangeSword"],
		game.ServerStorage.Skins.Common["RedSword"],
		game.ServerStorage.Skins.Common["WhiteSword"],
		game.ServerStorage.Skins.Common["YellowSword"]
	};
	uncommon = {
		game.ServerStorage.Skins.Uncommon["AwManSword"],
		game.ServerStorage.Skins.Uncommon["BloodiedSword"],
		game.ServerStorage.Skins.Uncommon["HealthSword"],
		game.ServerStorage.Skins.Uncommon["IronSword"],
		game.ServerStorage.Skins.Uncommon["PoisonSword"],
		game.ServerStorage.Skins.Uncommon["PumpkinSword"]
	};
	rare = {
		game.ServerStorage.Skins.Rare["CopperSword"],
		game.ServerStorage.Skins.Rare["CrimsonSword"],
		game.ServerStorage.Skins.Rare["GlassSword"],
		game.ServerStorage.Skins.Rare["HellfireSword"],
		game.ServerStorage.Skins.Rare["IceSword"]
	};
	legendary = {
		game.ServerStorage.Skins.Legendary["ForcefieldSword"],
		game.ServerStorage.Skins.Legendary["InvisibleSword"],
		game.ServerStorage.Skins.Legendary["PaperSword"]
	}
}
script.Parent.MouseButton1Click:Connect(function()
	--if the player has enough crates
	if plr.leaderstats.RareCrates.Value > 0 then
		--subtract one
		plr.leaderstats.RareCrates.Value -= 1
		--making stuff visible and invisible
		plr.PlayerGui.RareCrate.Frame.Visible = true
		script.Parent.Parent.Parent.Parent.Parent.Parent.Enabled = false
		--setting win to nil, it hasnt decided what it is yet.
		local win = nil
		for i = 1, 24 do
			task.wait(0.01)
			local clo = game.ServerStorage.Template:Clone()
			--randomness
			local random1 = math.random(1,100) 
			--if the random is inrange for a common reward
			if commonrange.Min < random1 and random1 < commonrange.Max or commonrange.Min == random1 or commonrange.Max == random1 then
				local random = math.random(1,7)
				task.wait(0.01)
				set("COMMON",Color3.new(0.75,0.75,0.75),Color3.new(0.6,0.6,0.6),rew.common[random],clo)
				--if the random is inrange for an uncommon reward
			elseif uncommonrange.Min < random1 and random1 < uncommonrange.Max or uncommonrange.Min == random1 or uncommonrange.Max == random1 then
				local random = math.random(1,6)
				set("UNCOMMON",Color3.new(0, 0.75, 0),Color3.new(0,0.6,0),rew.uncommon[random],clo)
				--if the random is inrange for an rare reward
			elseif rarerange.Min < random1 and random1 < rarerange.Max or rarerange.Min == random1 or rarerange.Max == random1 then
				local random = math.random(1,5)
				task.wait(0.01)
				set("RARE",Color3.new(0.75, 0, 0),Color3.new(0.6,0,0),rew.rare[random],clo)
				--if the random is inrange for an legendary reward (i know its common uncommon rare epic, but dont judge me pls 🥺)
			elseif legendaryrange.Min < random1 and random1 < legendaryrange.Max or legendaryrange.Min == random1 or legendaryrange.Max == random1 then
				local random = math.random(1,3)
				task.wait(0.01)
				set("LEGENDARY",Color3.new(0.8, 0.8, 0),Color3.new(0.6,0.6,0),rew.legendary[random],clo)
			end
			--if the winner is the nineteenth frame which is always the winner, set it to the winning frame
			if i == 19  then
				win = clo
			end
			--set the clones parent to the lootbox gui
			clo.Parent = plr.PlayerGui.RareCrate.Frame.Frame.hd
		end
		--tweening for the smooth movement
		plr.PlayerGui.RareCrate.Frame.Frame.hd:TweenPosition(UDim2.new(-4+(math.random(-1000,1)/100000),0,0,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quart,8)
		task.wait(9)
		--resetting the position for the next crate
		plr.PlayerGui.RareCrate.Frame.Frame.hd.Position = UDim2.new(0,0,0,0)
		--skin storage
		local ski = Instance.new("Folder")
		ski.Name = win.Name
		ski.Parent = plr.Skins
		--making stuff visible and invisible again
		plr.PlayerGui.RareCrate.Frame.Visible = false
		script.Parent.Parent.Parent.Parent.Parent.Parent.Enabled = true
		--finally, clearing all of the frames for the next crate
		for i, v in pairs(plr.PlayerGui.RareCrate.Frame.Frame.hd:GetChildren()) do
			if v:IsA("Frame") then
				v:Destroy()
			end
		end
	end
end)

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