Hello dear developers! I am here with an issue I cannot find answer to.
I’ve created an NPC that throws SuperBalls at a player, however, the aim of the NPC is very poor, and any player can dodge by thrown SuperBalls from coolkid NPC very easily by just walking away as shown in this video:
I tried to edit the SuperBall script, to maintain my goal to make the aim of my NPC much accurate. I’ve failed numerous times and could not find any answers from the internet, so I would like to seek for help.
Note that I am not a professional scripter, nor a newbie, I am an average scripter. I would like to see a readable and not hard to understand script to fix this issue!
Here is the script inside SuperBall tool:
local Tool = script.Parent
local Ball = Tool.Handle
local MouseLoc = Tool:WaitForChild("MouseLoc",10)
local Voice = script.Parent.Gotcha
local npc = script.Parent.Parent
function EnemyNear()
local nearestPlr = nil
local shortestDistance = math.rad(7200)
for _, plr in pairs(game.Players:GetPlayers()) do
if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
local distance = (npc.HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).magnitude
if distance <= shortestDistance then
nearestPlr = plr
shortestDistance = distance
end
end
end
return nearestPlr
end
function fire(direction)
Tool.Handle.Boing:Play()
local vCharacter = Tool.Parent
local vPlayer = script.Parent.Parent
local missile = Instance.new("Part")
local spawnPos = vCharacter.PrimaryPart.Position
spawnPos = spawnPos + (direction * 5)
missile.Position = spawnPos
missile.Size = Vector3.new(2,2,2)
missile.Velocity = direction * 165
missile.BrickColor = BrickColor.new("Really red")
missile.Shape = 0
missile.Locked = true
missile.BottomSurface = 0
missile.TopSurface = 0
missile.Name = "Cannon Shot"
missile.Elasticity = 1
missile.Reflectance = .2
missile.Friction = 0
Tool.Handle.Boing:Clone().Parent = missile
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = vPlayer
creator_tag.Name = "creator"
creator_tag.Parent = missile
missile.Parent = workspace
end
Tool.Enabled = true
function NPCactivate()
local animator = script.Parent.Parent.Humanoid:FindFirstChildOfClass("Animator")
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent;
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
print("Humanoid not found")
return
end
local plrPos = EnemyNear()
local targetPos = plrPos.Character.HumanoidRootPart.Position + Vector3.new(0, 0, 0)
local lookAt = (targetPos - character.Head.Position).unit
local animtrack = animator:LoadAnimation(script.Parent.Animation)
animtrack:Play()
task.wait(0.4)
fire(lookAt)
Ball.Transparency = 1
Voice:Play()
wait(1)
Ball.Transparency = 0
Tool.Enabled = true
end
while task.wait(1) do
NPCactivate()
end
Thank you.