There must be a better way to do this

I’ve heard somewhere that constant elseif statements are bad, and I would need an array, but since I’m new to scripting, I’d rather just learn this here and use it for my future scripts. It also doesn’t look too pretty… Any help is most appreciated.

Handle.Touched:Connect(function(hit)
	if hit.Parent:IsA("Model") and hit.Parent.Name == "Rock" and not on_cooldown then 
		OreStatus = "Rock"
		on_cooldown = true 
		task.wait(cooldown) 
		on_cooldown = false 
	elseif hit.Parent:IsA("Model") and hit.Parent.Name == "IronOre" and not on_cooldown then 
		OreStatus = "IronOre"
		on_cooldown = true 
		task.wait(cooldown) 
		on_cooldown = false 
	elseif hit.Parent:IsA("Model") and hit.Parent.Name == "GoldOre" and not on_cooldown then 
		OreStatus = "GoldOre"
		on_cooldown = true 
		task.wait(cooldown) 
		on_cooldown = false 
	elseif hit.Parent:IsA("Model") and hit.Parent.Name == "AzureOre" and not on_cooldown then 
		OreStatus = "AzureOre"
		on_cooldown = true 
		task.wait(cooldown) 
		on_cooldown = false 
	end
end)
Handle.Touched:Connect(function(hit)
	if hit.Parent:IsA("Model") and not on_cooldown then 
		OreStatus = hit.Parent.Name -- assign the rock name to the ore status
		on_cooldown = true 
		task.wait(cooldown) 
		on_cooldown = false 
	end
end)

I recommend using tags to make it easier to verify that the hit model is absolutely an ore object.

What this would look like with tags:

Handle.Touched:Connect(function(hit)
	if hit.Parent:IsA("Model") and hit.Parent:HasTag("Ore") and not on_cooldown then 
		OreStatus = hit.Parent.Name -- assign the rock name to the ore status
		on_cooldown = true 
		task.wait(cooldown) 
		on_cooldown = false 
	end
end)
2 Likes