Back at it with that AEwVSinspired game, I’m done with the math problem generator and now I’m on the AI of the teacher.
I want the teacher to move towards the player by 1 stud, simple as that but not simple enough for me to know how to do it. I’ve tried multiplying the target position by 1 but it won’t allow CFrames to be multiplied by numbers alone. I’ve also tried multiplying it by “CFrame.new(1,0,0)” but it just moves the teacher to the right of the player.
Here’s the code:
local closestPlayer = nil
local closestDistance = math.huge
wait(5)
repeat
wait(script.Parent:GetAttribute("WaitTime"))
--Getting Closest Player
for _, player in pairs(game.Players:GetPlayers()) do
local character = player.Character
local distance = (script.Parent.Position - character.HumanoidRootPart.Position).Magnitude
-- Check if current player is closer than the previous closest
if (not closestPlayer or distance < closestDistance) then
closestPlayer = player
closestDistance = distance
end
end
--Chase
local tween = game:GetService("TweenService"):Create(script.Parent, TweenInfo.new(script.Parent:GetAttribute("AttackTime"), Enum.EasingStyle.Linear), {CFrame = closestPlayer.Character.HumanoidRootPart.CFrame * CFrame.new(1 * script.Parent:GetAttribute("Speed"),0,0)}):Play()
--Use the information about the closest player (optional)
if closestPlayer then
print("Closest player:", closestPlayer.Name, "at distance:", closestDistance)
-- You can use closestPlayer and closestDistance for further actions
end
wait(script.Parent:GetAttribute("AttackTime"))
until false
sorry for taking 15 hours to respond i slept
i think i misexplained it. i want the teacher to move 1 stud TO the player, not a couple stud behind them.
ex: if the teacher is 3 studs away from the player, he will move to be 2 studs away from the player, since he’s moving 1 stud to the player.
thank you and i’m sorry for explaining it badly
Simple as tweening the position of the teacher to part1.position + (part2.position - part1.position).Unit. Here’s the entire code:
local closestPlayer = nil
local closestDistance = math.huge
wait(5)
repeat
wait(script.Parent:GetAttribute("WaitTime"))
--Getting Closest Player
for _, player in pairs(game.Players:GetPlayers()) do
local character = player.Character
local distance = (script.Parent.Position - character.HumanoidRootPart.Position).Magnitude
-- Check if current player is closer than the previous closest
if (not closestPlayer or distance < closestDistance) then
closestPlayer = player
closestDistance = distance
end
end
--Chase
local targetPos = script.Parent.position + (closestPlayer.Character.HumanoidRootPart.position - script.Parent.position).Unit * script.Parent:GetAttribute("Speed")
local tween = game:GetService("TweenService"):Create(script.Parent, TweenInfo.new(script.Parent:GetAttribute("AttackTime"), Enum.EasingStyle.Linear), {Position = targetPos}):Play()
--Use the information about the closest player (optional)
if closestPlayer then
print("Closest player:", closestPlayer.Name, "at distance:", closestDistance)
-- You can use closestPlayer and closestDistance for further actions
end
wait(script.Parent:GetAttribute("AttackTime"))
until false
only problem is that the teacher clips through walls