Help needed with turret on Procedurally Animated Spider

Hello everyone, lyrnu here. I need some help. :joy:

  1. What do you want to achieve? Keep it simple and clear!

I’m creating a procedurally generated spider and I am looking to add a turret on it’s back that moves to look at the closest player.

  1. What is the issue? Include screenshots / videos if possible!
    Well… for some reason, (probably because its a rig) when the turrets’ code looks at the nearest player, every other part moves as well. No, the turret isn’t it’s root/primary part. And yes - it’s pretty funny… :rofl:

    Here is the AimCode (scrapped from toolbox(very deprecated but I have tried other scripts and didn’t seem to function whatsoever)) :
function findTorso(pos)
	local torso = nil
	local dist = 100
	local child = workspace:children()
	for i=1, #child do
		if child[i].className == "Model" then
			local h = child[i]:findFirstChild("Humanoid")
			if h ~= nil then
				local check = child[i]:findFirstChild("Head")
				if check ~= nil then
					if (check.Position - pos).magnitude < dist then
						torso = check
						dist = (check.Position - pos).magnitude
					end
				end
			end
		end
	end
	return torso
end

game:GetService("RunService").Stepped:Connect(function()
	local torso = findTorso(script.Parent.Position)
	if torso ~= nil then
		script.Parent.CFrame = CFrame.new(script.Parent.Position, torso.Position)
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes I did which is why I’m looking for your help. I think @dthecoolest did something on Turrets. Not sure if it works for parts attached as a rig.

Thank you for your time! :grin:

I am going to bump this - no replies yet… :frowning:

Change the Motor6D’s offset CFrame instead of the CFrame of the turret part.

1 Like

How would that look inside code?

Here:

-- Set a variable for the Motor6D. Part0 is the part where turret is attached to
local motor6D

-- This stuff inside the if torso ~= nil part
local targetPosition = torso.Position
local turretCF = motor6D.Part0.CFrame
local direction = (turret.Position - targetPosition).Unit

local lookCF = CFrame.new(turretCF.Position, turretCF.Position + direction)
motor6D.C0 = turretCF:ToObjectSpace(lookCF)

Idk if it works, but try changing up the values if it doesn’t. I think this is the correct approach, though I can’t run it right now, I’m on laptop.

1 Like