Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way to simplify this function?

I'm remaking a gun system I made a while ago, and I'm optimizing it as much as possible. I'm trying to add particle effects based on material. Here's the function I made:

function CalculateTexture(Hit)
	local Prefix = "rbxasset://"
	local ReturnThis = Prefix..Particle.Fabric
	if Hit.Material == Enum.Material.Brick or 
		Hit.Material == Enum.Material.Cobblestone or 
		Hit.Material == Enum.Material.Concrete or
		Hit.Material == Enum.Material.Granite or 
		Hit.Material == Enum.Material.Marble or
		Hit.Material == Enum.Material.Pebble or
		Hit.Material == Enum.Material.Slate then
		ReturnThis = Prefix..Particle.Concrete
	elseif Hit.Material == Enum.Material.Grass then
		ReturnThis = Prefix..Particle.Grass
	elseif Hit.Material == Enum.Material.CorrodedMetal or
		Hit.Material == Enum.Material.DiamondPlate or
		Hit.Material == Enum.Material.Foil or
		Hit.Material == Enum.Material.Metal then
		ReturnThis = Prefix..Particle.Metal
	elseif Hit.Material == Enum.Material.Sand or
		Hit.Material == Enum.Material.Plastic or
		Hit.Material == Enum.Material.SmoothPlastic or
		Hit.Material == Enum.Material.Neon then
		ReturnThis = Prefix..Particle.Sand
	elseif Hit.Material == Enum.Material.Wood or
		Hit.Material == Enum.Material.WoodPlanks then
		ReturnThis = Prefix..Particle.Wood
	end
	return ReturnThis
end

It simply just figures out what material it is and returns the texture based on material... Is there any simpler way to do this? I want it to be as optimized as possible, meaning this large function may simply not do...

4 comments

3

You could try to mimic a switch http://lua-users.org/wiki/SwitchStatement

0

That link is awesome ^

0

It worked pretty well. Unfortunately, it made the script longer. I may just stick to one texture instead

0

It should be able to make the function smaller. It might add some tables outside of the function... But Good Luck man!

1 answer

✓ Accepted answer

You could make a table for all the possible returns you want and what materials correspond to them. I'd say the code is easier to manipulate now, and you save 10 lines so yay.

function CalculateTexture(Hit)
	local Prefix = "rbxasset://"
	local ReturnThis = Prefix..Particle.Fabric
	local returns = {
		{{"Brick","Cobblestone","Concrete","Granite","Marble","Pebble","Slate"},"Concrete"},
		{{"Grass"},"Grass"},
		{{"CorrodedMetal","DiamondPlate","Foil","Metal"},"Metal"},
		{{"Sand","Plastic","SmoothPlastic","Neon"},"Sand"},
		{{"Wood","WoodPlanks"},"Wood"}
	}
	for _,a in next,returns do
		for _,v in next,a[1] do
			if tostring(Hit.Material) == v then
				ReturnThis = Prefix..Particle[a[2]]
			end
		end
	end
	return ReturnThis
end

Note: Idk if everyone's resolution produces the same results, but when I viewed the code box above line 5 was cut off. If it's the same for you(and you care about seeing it) then just copy it and view it in studio or notepad or something

2 comments

1

The question is easily viewable. Thanks Goulstem! I just have one more question... Why use in next instead of in pairs? don't they do the same thing?

0

Yes, they do do the same thing. Pairs uses 'next' as its iterator function so some people, like me, prefer to call it directly. The choice for which one to use is completely user preference; moreover, using one rather than the other will not really have any advantages, or disadvantages.

This discussion is preserved as it was. New answers, comments, and votes are closed.