I’ve made an NPC which chases nearby players that moves via changing the CFrame of the primary part of its model. It mostly works as intended, however when the player jumps, the NPC begins to levitate, and eventually moves back down. I want the NPC to stay upright on top of the surface it is standing on. I tried looking through video tutorials and DevForum but there’s really no solution I can find… Been a while since I programmed this NPC too so I wouldn’t be 100% sure to be able to explain all of the code.
coding shenanigans, this is all just inside 1 script
local EvilLeafy = script.Parent
local Scream = EvilLeafy.Body.Scream
local Teleport = EvilLeafy.Body.Teleport
local MaxDistance = 200
local Vignette = script:FindFirstChild("VignetteEffect")
function Kill(part)
part.Parent.Humanoid.Health = 0
if not Scream.Playing then
Scream:Play()
end
end
function findNearestCharacter(leafPosition)
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character and player.Character:FindFirstChild("Humanoid") and player.Character:FindFirstChild("Humanoid").Health > 0 then
local distance = (player.Character.HumanoidRootPart.Position - leafPosition).Magnitude
if distance < MaxDistance then
return player.Character
else
return false
end
else
return false
end
end
end
function teleportToCharacter()
local character = findNearestCharacter(EvilLeafy.Hitbox.Position)
if character then
local distance = character.HumanoidRootPart.Position - EvilLeafy.Hitbox.Position
local distance2 = (character.HumanoidRootPart.Position - EvilLeafy.Hitbox.Position).Magnitude
local charOrientation = character.HumanoidRootPart.Orientation
local offset = distance.Unit * 16
local lookAt = CFrame.lookAt(EvilLeafy.Hitbox.Position, character.HumanoidRootPart.Position)
if distance2 <= 20 then
EvilLeafy:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)
Kill(character.HumanoidRootPart)
else
EvilLeafy:SetPrimaryPartCFrame(lookAt + offset)
end
Teleport:Play()
if game:GetService("Players"):GetPlayerFromCharacter(character).PlayerGui:FindFirstChild(Vignette.Name) then
game:GetService("Players"):GetPlayerFromCharacter(character).PlayerGui:FindFirstChild(Vignette.Name):Destroy()
end
local clone = Vignette:Clone()
clone.Parent = game:GetService("Players"):GetPlayerFromCharacter(character).PlayerGui
end
end
EvilLeafy.Hitbox.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
Kill(part)
end
end)
while true do
task.wait(1)
if script.Parent.Hitbox.Parent == nil then
EvilLeafy:Destroy()
end
teleportToCharacter()
end