Part not deleting?

Hello! I have this shape rarity, but it doesn’t delete after they click again. Please help!

local rs = game:GetService("ReplicatedStorage")
local Shapes = rs:WaitForChild("Shapes", 5):GetDescendants()

local Spawner = workspace:WaitForChild("Spawner")
local Chance = require(rs:WaitForChild("Chance"))

local gui = script.Parent
local Button = gui.ShapeButton

Button.MouseButton1Down:Connect(function()
	local Rarity = Chance.PickRarity("Rarities")
	
	for i, v in pairs(Shapes) do
		if v:IsA("BasePart") or v:IsA("MeshPart") then
			local Folder_Shapes = v.Parent
			
			if Rarity == Folder_Shapes.Name then
				local HasShape = false
				
				local RandomShape = Folder_Shapes:GetChildren()[math.random(1, #Folder_Shapes:GetChildren())]:Clone()
				
				if not HasShape then
					HasShape = true
					print(Rarity)
					print(HasShape)
					RandomShape.Parent = Spawner
					RandomShape.CFrame = CFrame.new(Spawner.CFrame.X, Spawner.CFrame.Y + 5, Spawner.CFrame.Z)
					
					print(RandomShape.CFrame)
					print(RandomShape)
					
					break
					
				elseif HasShape then
					RandomShape:Destroy()
					HasShape = false
					print("False")
				end
			end
		end
		
	end
	
end)

Rewrote it but still doesnt work

local rs = game:GetService("ReplicatedStorage")
local Shapes = rs:WaitForChild("Shapes", 5):GetDescendants()

local Spawner = workspace:WaitForChild("Spawner")
local Chance = require(rs:WaitForChild("Chance"))

local gui = script.Parent
local Button = gui.ShapeButton

local HasShape = false

Button.MouseButton1Down:Connect(function()
	local Rarity = Chance.PickRarity("Rarities")
	
	for i, v in pairs(Shapes) do
		if v:IsA("BasePart") or v:IsA("MeshPart") then
			local Folder_Shapes = v.Parent
			
			if Rarity == Folder_Shapes.Name then
				
				local RandomShape = Folder_Shapes:GetChildren()[math.random(1, #Folder_Shapes:GetChildren())]:Clone()
				
				if not HasShape then
					HasShape = true
					print(Rarity)
					print(HasShape)
					RandomShape.Parent = Spawner
					RandomShape.CFrame = CFrame.new(Spawner.CFrame.X, Spawner.CFrame.Y + 5, Spawner.CFrame.Z)
					
					print(RandomShape.CFrame)
					print(RandomShape)
					
					break
					
				else
					RandomShape:Destroy()
					HasShape = false
					print("False")
				end
			end
		end
		
	end
	
end)

Hello again. Let me test the script. Does this script rely on any other scripts to work?

Hi! Remember you again lol (thanks for the help btw). Yeah it relies on a module script

local Chance = {}

local Rarities = {
	Common = 0, -- 60% chance
	Rare = 0.6, -- 40% chance
	Epic = 0.8, -- 20% chance
	Legendary = .9, -- 10% chance
	--Mythic = 0.99, -- 1% chance
}



function Chance.PickRarity()
	local Index = math.random()
	local HighestRarity = "Common"

	for RarityName, Value in pairs(Rarities) do
		if Index >= Value and Value >= Rarities[HighestRarity] then
			HighestRarity = RarityName
		end
	end

	return HighestRarity
end

return Chance

Alright. Thanks for that input. I’ll test the code and see if I can do anything to help you.

Edit: I’ve gotten it to work on my end. Still testing some things.

1 Like

Is the part supposed to be destroyed if the player clicks the button twice?

When they reroll so let say they roll a common. Next time they roll, the common that they rolled will be deleted. So I guess yeah.

Got it. I think I may know how to fix your issue, but I might need more information. Give me a minute, and I’ll get back with you.

1 Like

Okay! Once again, thanks for the help!

1 Like

No problem! I didn’t have to change your ModuleScript, but I did tinker with your LocalScript code. Try this:

local rs = game:GetService("ReplicatedStorage")
local Shapes = rs:WaitForChild("Shapes", 60):GetDescendants()

local Spawner = workspace:WaitForChild("Spawner")
local Chance = require(rs:WaitForChild("Chance"))

local gui = script.Parent
local Button = gui.ShapeButton

local HasShape = false
local OldShape = nil

print("Everything loaded.")

Button.MouseButton1Down:Connect(function()
	local Rarity = Chance.PickRarity("Rarities")
	print("rare")
	for i, v in pairs(Shapes) do
		if v:IsA("BasePart") or v:IsA("MeshPart") then
			local Folder_Shapes = v.Parent

			if Rarity == Folder_Shapes.Name then

				local RandomShape = Folder_Shapes:GetChildren()[math.random(1, #Folder_Shapes:GetChildren())]:Clone()

				if HasShape ~= true then
					HasShape = true
					print(Rarity)
					print(HasShape)
					RandomShape.Parent = Spawner
					RandomShape.CFrame = CFrame.new(Spawner.CFrame.X, Spawner.CFrame.Y + 5, Spawner.CFrame.Z)

					print(RandomShape.CFrame)
					print(RandomShape)
					OldShape = RandomShape
					break

				elseif HasShape == true then
					OldShape:Destroy()
					HasShape = false
					print("False")
				end
			end
		end

	end

end)
1 Like

Worked perfectly! Thanks for the help!

2 Likes

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