Hi, to start off, I’m pretty sure that these two questions I have have already been answered in some form but I’m not even sure how to phrase the question to actually search it up. I’ll be posting a video show casing both problems and stating what I did to try to solve them.
First question is about Region3, Im using it as my way of damaging Npcs. When attacking the Npcs though, I noticed that the attacks wouldnt connect with the Npc so I went ahead and maybe a visualized hitbox of the the attack shown in the video. It shows that the hitbox isn’t actually learning up the characters model and is instead on some sort of grid and holds the same orientation no matter what direction the player is facing. How do I go about lining things up so the Region3’s orientation is the same as the players?
Second question is about the Npc’s MoveTo function. I noticed the same problem in the roblox game Ro-Ghoul where the Npc would run away from the player and mimic the players movements. In my case, I have the Npc record its original location in the world and move back to the location after following the player a certain distance. I have no clue what the issue is here, I’ve tried both Move to and Walk to Part functions and I got the same result. Notice it only happens when the Npc tries returning it its original point. How do I fix this problem?
https://www.youtube.com/watch?v=A9AqECrT9Qk
Region3 code, this is code I made for my melee weapons
enable = true
combo = 0
script.Parent.OnServerEvent:Connect(function(player, attack, range)
local c = player.Character
local stats = player.stats
local cooldown = .2
local knockback = 1
if enable == false then return end
if c.Humanoid.Health > 0 then
if attack == "Combat" and not stats.action.Value then
enable = false
stats.action.Value = true
if combo == 0 then
local anim1 = Instance.new("Animation")
anim1.AnimationId = "rbxassetid://04946762293"
local anim1play = c.Humanoid:LoadAnimation(anim1)
anim1play:Play()
combo = 1
cooldown = .2
knockback = 1
else
if combo == 1 then
local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://04946762293"
local anim2play = c.Humanoid:LoadAnimation(anim2)
anim2play:Play()
combo = 2
cooldown = .1
knockback = 1
else
local anim3 = Instance.new("Animation")
anim3.AnimationId = "rbxassetid://04946762293"
local anim3play = c.Humanoid:LoadAnimation(anim3)
anim3play:Play()
combo = 0
cooldown = .3
knockback = 50
end
end
local bv1 = Instance.new("BodyVelocity",c.Humanoid)
bv1.MaxForce = Vector3.new(5000,5000,5000)
bv1.Velocity = c.HumanoidRootPart.CFrame.lookVector*knockback
game.Debris:AddItem(bv1,0.1)
wait (.1)
local region = Region3.new(range - Vector3.new(5,5,5),range+Vector3.new(5,5,5))
local hitbox = Instance.new("Part",c)
local hitboxweld = Instance.new("WeldConstraint",hitbox)
hitbox.Position = range
hitbox.Size = Vector3.new(5,5,5)
hitbox.Orientation = c.HumanoidRootPart.CFrame.LookVector
hitbox.BrickColor = BrickColor.new("Really red")
hitbox.Transparency = .5
hitbox.CanCollide = false
hitboxweld.Part0 = c.HumanoidRootPart
hitboxweld.Part1 = hitbox
game.Debris:AddItem(hitbox,0.2)
local RTable = workspace:FindPartsInRegion3(region, nil, 20)
for i,v in pairs(RTable) do
if v.Parent:findFirstChild("Humanoid") and v.Parent:findFirstChild("Deb")== nil and v.Parent:FindFirstChild("Npc") then
if v.Parent:findFirstChild("Humanoid").Health > 0 then
local Deb = Instance.new("BoolValue", v.Parent)
Deb.Name = "Deb"
game.Debris:AddItem(Deb,0.2)
local damage = .5+((stats.blunt.Value*.35)*(stats.strength.Value*.3))
v.Parent.Humanoid:TakeDamage(damage) --Damage Taken
local S = Instance.new("Sound",v)
S.SoundId ="rbxassetid://367499850"
S.PlaybackSpeed = math.random(90,110)/100
S:Play()
local bv = Instance.new("BodyVelocity",v)
bv.MaxForce = Vector3.new(25000,25000,25000)
bv.Velocity = c.HumanoidRootPart.CFrame.lookVector*(stats.blunt.Value*0.25)
game.Debris:AddItem(bv,0.1)
print (damage)
stats.blunt.Value = stats.blunt.Value + 0.005 + (stats.blunt.Value*0.001)
stats.strength.Value = stats.strength.Value + 0.0005 + (stats.blunt.Value*0.001)
--if v.Parent.Humanoid:FindFirstChild("tag") ~= nil then
local tag = Instance.new("ObjectValue",v.Parent.Humanoid)
tag.Name = "tag"
tag.Value = player
--end
end
end
end
wait (cooldown)
enable = true
stats.action.Value = false
end
end
spawn(function()
local oldcombo = combo
wait(0.5)
if combo == oldcombo then
combo = 0
end
end)
end)
Npc code, a lot of the variables at the top were for later coding but I never got around to it
local npc = script.Parent
local o = require(script.Parent:WaitForChild("EnemyModule"))
local debouncemove = false
local returning = false
local anchor = script.Parent.HumanoidRootPart.Position
local MaxDistance = 70
local meleerange = 2
--Npc Parts
local name = o["Name"]
local humanoid = npc:WaitForChild("Humanoid")
local health = o["Health"]
local movementspeed = o["Movement"]
local root = npc:WaitForChild("HumanoidRootPart")
local head = npc:WaitForChild("Head")
local torso = npc:WaitForChild("Torso")
local rarm = npc:WaitForChild("Right Arm")
local larm = npc:WaitForChild("Left Arm")
local rleg = npc:WaitForChild("Right Leg")
local lleg = npc:WaitForChild("Left Leg")
function melee()
for i,v in pairs(workspace:GetChildren()) do
local h = v:FindFirstChild("Humanoid")
local r = v:FindFirstChild("HumanoidRootPart")
local n = v:FindFirstChild("Npc")
if h and r and not n then
if (r.Position - root.Position).magnitude < o["MRange"] and h.Health > 0 then
local anim1 = Instance.new("Animation")
anim1.AnimationId = "rbxassetid://4925007598"
local anim1play = humanoid:LoadAnimation(anim1)
anim1play:Play()
wait(.1)
h:TakeDamage(o["MDamage"])
end
end
end
end
--Target Finding Functions
function findtarget()
local target
local dist = MaxDistance
for i,v in pairs(workspace:GetChildren()) do
local humanoid = v:FindFirstChild("Humanoid")
local rootpart = v:FindFirstChild("HumanoidRootPart")
local npchumanoid = v:FindFirstChild("Npc")
if humanoid and rootpart and not npchumanoid then
if (rootpart.Position - root.Position).magnitude < dist and humanoid.Health > 0 then
dist = (rootpart.Position - root.Position).magnitude
target = rootpart
end
end
end
return target
end
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = MaxDistance
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
--wait(math.random(0,5)/10)
while true do
wait(0.5)
melee()
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil and (script.Parent.HumanoidRootPart.Position-anchor).magnitude < MaxDistance and not returning then
debouncemove = false
script.Parent.Humanoid:MoveTo(target.Position, target)
print ("Move")
wait(1)
debouncemove = true
--script.Parent.Humanoid:MoveTo(Vector3.new(0, 0, 0))
end
if debouncemove then
local target = findNearestTorso(script.Parent.Torso.Position)
if (script.Parent.HumanoidRootPart.Position-anchor).magnitude > MaxDistance then
script.Parent.Humanoid:MoveTo(Vector3.new(0, 0, 0))
debouncemove = false
returning = true
wait(.05)
print("Far")
script.Parent.Humanoid:MoveTo(anchor)
wait(5)
debouncemove = true
returning = false
end
end
end
I’m sure a lot of the terms I used in this post were in correct, but these two problems and the fact I couldn’t post in these forums put me off from coding for 2 months. Thanks for any help and your time guys