Help with script

ok so i know bodygyro is deprecated so if i were to use bodygryo.D what would i replace it with

local SererStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}

local function FindNeartestTarget(newTower)
	local maxDistance = 25
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance then
			print(target.Name, "is nearest target found so far")
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget

end


function tower.Attack(newTower)
	local target = FindNeartestTarget(newTower) 	
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		animateTowerEvent:FireAllClients(newTower, "Attack")
		target.Humanoid:TakeDamage(25) 	
		end
		task.wait(1)

		
		tower.Attack(newTower)
	end 
	


function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		
		local bodyGyro = Instance.new("AlignOrientation")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	
		bodyGyro.D = 0
		bodyGyro.CFrame = newTower.HumandoidRootPart.CFrame
		bodyGyro.Parent = newTower.HumanoidRootPart
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Towers"
			end
			end
			coroutine.wrap(tower.Attack)(newTower)
			

	else
		warn("Requested tower does not exist")

		end
	
end
	

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower
1 Like

Try Humanoid:MoveTo(), maybe that will work.

1 Like

how would i use this in my script, can you help, im new to scripting

1 Like

also im using AlignOrientation instead of bodygyro but yeah i dont know what to replace bodygryo.D with

1 Like

Try this script:

local SererStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}

local function FindNeartestTarget(newTower)
	local maxDistance = 25
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance then
			print(target.Name, "is nearest target found so far")
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget

end


function tower.Attack(newTower)
	local target = FindNeartestTarget(newTower) 	
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		newTower.Humanoid:MoveTo(newTower.HumanoidRootPart.Position)
		animateTowerEvent:FireAllClients(newTower, "Attack")
		target.Humanoid:TakeDamage(25) 	
		end
		task.wait(1)

		
		tower.Attack(newTower)
	end 
	


function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers

		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Towers"
			end
			end
			coroutine.wrap(tower.Attack)(newTower)
			

	else
		warn("Requested tower does not exist")

		end
	
end
	

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower

BodyGyro was superceded by AlignOrientation a few years ago, the big difference between them being that AlignOrientation requires an Attachment to function properly, while BodyGyro does not.

AlignOrientation maintains much of the same functions as BodyGyro, however BodyGyro.D doesnt appear to have any replacements. BodyGyro.P does however, with it being AlignOrientation.Responsiveness.

this doesnt allow the tower to face the target while killing it ( what im trying to achieve)

Are you aware that you still have access to body gyros or do you just not want deprecated objects in your game? if you don’t know how to access it then just do the Instace.new(“Instance”) command.

This should fix the issue with that, lmk if it works.

local SererStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}

local function FindNeartestTarget(newTower)
	local maxDistance = 25
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance then
			print(target.Name, "is nearest target found so far")
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget

end


function tower.Attack(newTower)
	local target = FindNeartestTarget(newTower) 	
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		newTower.HumanoidRootPart.CFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target) -- Actually LOOK at the target
		newTower.Humanoid:MoveTo(newTower.HumanoidRootPart.Position)
		animateTowerEvent:FireAllClients(newTower, "Attack")
		target.Humanoid:TakeDamage(25) 	
		end
		task.wait(1)

		
		tower.Attack(newTower)
	end 
	


function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Towers"
			end
			end
			coroutine.wrap(tower.Attack)(newTower)
			

	else
		warn("Requested tower does not exist")

		end
	
end
	

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower

just gives me this error

I see what I did.

local SererStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}

local function FindNeartestTarget(newTower)
	local maxDistance = 25
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance then
			print(target.Name, "is nearest target found so far")
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget

end


function tower.Attack(newTower)
	local target = FindNeartestTarget(newTower) 	
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		newTower.HumanoidRootPart.CFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.Position) -- Actually LOOK at the target
		newTower.Humanoid:MoveTo(newTower.HumanoidRootPart.Position)
		animateTowerEvent:FireAllClients(newTower, "Attack")
		target.Humanoid:TakeDamage(25) 	
		end
		task.wait(1)

		
		tower.Attack(newTower)
	end 
	


function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Towers"
			end
			end
			coroutine.wrap(tower.Attack)(newTower)
			

	else
		warn("Requested tower does not exist")

		end
	
end
	

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower

I don’t know what my small brain was thinking.


im confused because position is a member of the target

local SererStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local animateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}

local function FindNeartestTarget(newTower)
	local maxDistance = 25
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < maxDistance then
			print(target.Name, "is nearest target found so far")
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget

end


function tower.Attack(newTower)
	local target = FindNeartestTarget(newTower) 	
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		newTower.HumanoidRootPart.CFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position) -- Actually LOOK at the target
		newTower.Humanoid:MoveTo(newTower.HumanoidRootPart.Position)
		animateTowerEvent:FireAllClients(newTower, "Attack")
		target.Humanoid:TakeDamage(25) 	
		end
		task.wait(1)

		
		tower.Attack(newTower)
	end 
	


function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Towers"
			end
			end
			coroutine.wrap(tower.Attack)(newTower)
			

	else
		warn("Requested tower does not exist")

		end
	
end
	

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower

Tell me if this works, I’m not exactly sure how my brain is so smol sometimes

Thank you soo much, to show my thanks I just wanted to give a little motivational robux

1 Like

Wait, you didn’t need to do that, but thank you so much! God bless you!

1 Like

this was wholesome in a way…

30charlimirtttt

2 Likes