My script works it’s just that i made it work in the worst possible way, by repeating the same lines over and over again because whatever i tried to do to shorten it broke it
local ray = Ray.new(player.Character.HumanoidRootPart.Position,(victim.Character.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).unit * 20)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
if part then
if part.Name == "BlockBlock" then
elseif part.Name == "RightLowerArm" then
elseif part.Name == "LeftLowerArm" then
elseif part.Name == "RightHand" then
elseif part.Name == "LeftHand" then
elseif part.Name == "MeshPart" then
end
I am firing a ray from player HRP to target HRP to check if the player attacked from the front and was blocked or not, these parts all represent a block, but i am unsure how to shorten this so that it’s all just 1 if statement or for loop because what i tried only made things worse
Nested if statements are always very bad and messy to look at and there are ways to reduce the character or line count, but that will make the code even more messy and it will perform worse. Remember, Performance > Less Code. You could improve the code indentation tho.
local ray = Ray.new(player.Character.HumanoidRootPart.Position,(victim.Character.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).unit * 20)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
if part then
if part.Name == "BlockBlock" then
elseif part.Name == "RightLowerArm" then
elseif part.Name == "LeftLowerArm" then
elseif part.Name == "RightHand" then
elseif part.Name == "LeftHand" then
elseif part.Name == "MeshPart" then
end
Better spacing makes the code much more readable and it just looks better.
Hope this helped you! If so, feel free to set this as solution
unfortunately no this does not help me at all, in instrest of readability i’ve deleted the lines that are supposed to run depending on the part hit, there’s like 100 lines and i was hoping i could just check that it’s one of these parts and then just write the rest once
If you only want to see if something was blocking the attack or not then I suggest you check if the part is the victim’s limb or stuff like that.
local ray = Ray.new(player.Character.HumanoidRootPart.Position,(victim.Character.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).unit * 20)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
if part and part:IsA("BasePart") and part.Parent == victim then
print(part) -- one of the victims body part
-- Do something here.
end
the thing is the only limbs that count as a block are the hands and lower arms, the rest counts as a hit, this is because the part i use as “Block” must be close enough to the player so that you can’t just hug him and hit through the block, which means that the lower arms and hands are exposed
Maybe try putting the names of any part you want to block and check if it exist in a table? This way the code is more cleaner, easy to sort and epic(er). Sorry if my explanation is a bit weird I’m not good at English.
local BlockedPartName = {
"RightLowerArm";
"RightHand";
"LeftLowerArm";
"LeftHand";
}
if part then
if not table.find(BlockedPartName, part.Name) then
-- Hit the part that wasn't meant to be blocked
else
-- Blocked
end
end
i think ik what u trying to do, u trying to do like in CS where theres different damage for different part types?
if yes use dictionaries
local parts = {
Head = 50;
UpperTorso = 25;
LowerTorso = 15;
etc = 0;
}
if part then
local dmg = parts[part]
-- code
end
boom code shortened 300x times, also if u want even shorter u can use modules (modules are used to store any amount and type of info that u can read from any script later like weapons info like ammo, damage etc…)
module code:
local module = {}
module.Head = 50
module.Torso = 25
-- etc u already know dis part
return module
script code:
local module = require(script:WaitForChild("ModuleScript")) -- u can chosoe other directory and name
if part then
local dmg = module[part]
end
boom code shortened 600x times
ik it looks like the first code but the table is now separated taking only 1 line in ur code instead
Thank you very much. This is exactly what i was hoping for. I had the idea right from the start to use a table and i did but i didn’t think to use table.find but used loops instead, thanks again my code got 100 lines shorter
no dmg is consistent. My blocking system works like this. when you block you turn on a value that confirms that you are blocking as well as spawn a part in front of yourself that represents it. When you are attacked by a player a ray is fired from his HRP to yours. If the ray doesn’t hit the block that means you were attacked from the side or back and couldn’t block so you got hit. Finally got it working now, works like a charm