Five-way attack in fighting game not working due to script de-bloating attempt

So I’m trying to make this attack in this fighting game that I’m working on that sends out five sharpened bone attacks each in their own directions while they spin around as they move in a straight line.

Normally this would be easy for me to program, albeit in an unoptimized manner, but I have been making an effort to try and de-bloat my scripts lately to try and prevent as much lag as possible in my games.

The issue being though, whenever I try to test the attack by clicking the ‘Run’ button, the bones just congregate into one singular head part and don’t move after that. They used to move along with the head part before I added in code to try and make them spin around as they’re connected to the head part that’s moving in a straight line.

Here is the code for the attack:

folder=script.Parent
script.Parent.Head.hi:Play()
task.wait(1)
script.Parent.delete.Disabled = false --Enables the script that causes the attack to delete itself once a certain amount of time has passed

local bones = { --The bones in the attack, supposed to be connected to their respective head parts
	bone1 = 1,
	bone2 = 2,
	bone3 = 3,
	bone4 = 4,
	bone5 = 5
}

local heads = { --The head parts in the attack, the respective bones are supposed to go on them so they can go in their own separate directions
	head1 = 1,
	head2 = 2,
	head3 = 3,
	head4 = 4,
	head5 = 5
}

while true do
	for _, bone in ipairs(folder:GetChildren()) do
		if bones[bone.Name] then
			for _, head in ipairs(folder:GetChildren()) do
				if heads[head.Name] then
					bone.Position = head.Position --Supposed to put the bones in the place of the head parts so they can spin without flying off track of their straight path
				end
			end
		end
	end
	
	for _, bone in ipairs(folder:GetChildren()) do
		bone.CFrame *= CFrame.Angles(0,0,-0.1) --Supposed to spin the bones
	end
	
	for _, head in ipairs(folder:GetChildren()) do
		if heads[head.Name] then
			head.CFrame *= CFrame.new(0,0,-2)  --Moves the head parts
		end
	end
	task.wait()
end

Whenever I run the attack, an error will pop up in the console:

CFrame is not a valid member of Script "Workspace.Bones.Script"

I tried changing “bone.CFrame *= CFrame.Angles(0,0,-0.1)” in the script to “bones.CFrame *= CFrame.Angles(0,0,-0.1)” to see if that would work, but it provided this error in the console instead:

Workspace.Bones.Script:34: invalid argument #1 (CFrame expected, got nil) 

If anybody has any advice for this issue then please let me know. Any and all help will be greatly apprecated, thank you ! ^ ^

You need to check if bone is a BasePart in this loop. Since your script is in the same folder as the bones, it is also being iterated over, and its CFrame (non-existent, since it’s a script) is trying to accessed. You’re already doing something similar with if bones[bone.Name] and if heads[head.Name], but checking the class name using IsA() is a more elegant solution.

Edit: The purpose of checking an object’s class name is to ensure the property you are trying to access exists.

1 Like

Ok so I tried using IsA() in the script’s code and now the bones don’t teleport at all, now it provides a new error in the console:

Workspace.Bones.Script:24: attempt to call missing method 'IsA' of table

This is what I put in the script, I didn’t change much of the code so I’ll only be putting the parts that I did change:

while true do
	for _, bone in ipairs(folder:GetChildren()) do
		if bones:IsA("UnionOperation") then
			for _, head in ipairs(folder:GetChildren()) do
				if heads:IsA("Part") and heads.Name ~= "Head" then
					bone.Position = head.Position
				end
			end
		end
	end

Change bones:IsA(...) to bone:IsA(...).

Edit: and similarly heads.

1 Like

Ok so I did that just now, they are teleporting again but now they’ve all teleported to a different head part, and I did end up receiving the same error in the console as mentioned in the original post:

CFrame is not a valid member of Script "Workspace.Bones.Script"

The error says it happened at line 34:

for _, bone in ipairs(folder:GetChildren()) do
	bone.CFrame *= CFrame.Angles(0,0,-0.1) --Line 34
end

Make sure you add an IsA() check in that loop as well. I’d also separate bones and heads into different folders. The outer and inner loops in the snippet you sent previously are actually iterating over the exact same instances, save renaming the value variable.

1 Like

Ok I added the IsA() loop to all parts of the code that needed it, the bones do move again and are actually spinning now, but they’re still all teleporting and moving along the same head part rather than their assigned individual ones?

If I’m understanding what you’re trying to do correctly, there’s going to need to be some changes to your logic. First, don’t get your bones and heads using GetChildren(), since the order that the children are returned in is not guaranteed. Second, change your bones and heads tables to contain references to each bone and head (e.g. local bones = {reference.To.Bone1, ...}). As long as the index of each bone matches the index of the head you want it to teleport to, you now have parallel lists, so the nested loop you had before becomes:

for i, bone in ipairs(bones) do
    bone.Position = heads[i].Position
end

Finally, update the rest of the script to accommodate the new logic.

1 Like

Gotcha, so I put the:

local bones = {reference.To.Bone1, ...}

code to replace the original list of bones in the code, but it’s putting a red line under reference and saying that reference is an unknown global in the code?

Not literally reference.To.Bone1 but the location of the bone in the hierarchy. Something like

local PartsFolder = script.Parent

local bones = {
    PartsFolder.Bones.Bone1,
    PartsFolder.Bones.Bone2,
    PartsFolder.Bones.Bone3,
    PartsFolder.Bones.Bone4,
    PartsFolder.Bones.Bone5
}

assuming your script is in a folder that contains a subfolder named Bones which contains bones named like BoneN where N is an integer between 1 and 5.

1 Like

Ok I created a new folder inside of the attack itself to put the bones in, put the code in and now it works !
Thank you for your help ! ^ ^

1 Like

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