Pathfinder is making my NPC follow my oldest position only - lua

I am trying to make a maze/horror game. I used an online template in the Roblox library as my enemy. I used pathfinder as you will see in the code below. It's finding me like it's supposed to, except it only goes for my LAST position. As you can see in the image below, it completely skipped me, went to my LAST position, then started chasing me. I don't know why it only goes for my last position, and not my current position.
local PathFindingS = game:GetService("PathfindingService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
local Players = game:GetService("Players")
game.Workspace.Fruity.Humanoid.WalkSpeed = 60
--To calculate the path
while wait() do
for i, player in pairs(game.Players:GetPlayers()) do
local character = game.Workspace:WaitForChild(player.Name)
local characterPos = character.PrimaryPart.Position
local path = PathFindingS:CreatePath()
path:ComputeAsync(rootPart.Position, characterPos)
game.Workspace.Fruity.HumanoidRootPart:SetNetworkOwner(nil)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6,0.6,0.6)
part.Position = waypoint.Position + Vector3.new(0,2,0)
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end

Your NPC's pathfinding updates when you call path:ComputeAsync(rootPart.Position, characterPos). The reason it is not updating more frequently is that you are blocking the start of the next loop with the last line : humanoid.MoveToFinished:Wait()
Your code is telling the NPC that it must walk to every single point between every single player, which could take minutes at a time, before ever calculating the path again.
The way to fix this is to make it so that the path can be recalculated quickly and asynchronously. To do this, try something like this :
local PathfindingService = game:GetService("PathfindingService")
local PlayerService = game:GetService("Players")
local humanoid = script.Parent:WaitForChild("Humanoid")
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
-- create a place where pathfinding orbs can be created and destroyed quickly
local shouldDebugPath = true
local OrbFolder = Instance.new("Folder", game.Workspace)
OrbFolder.Name = "Debug - Orbs"
local OrbTemplate = Instance.new("Part")
OrbTemplate.Shape = Enum.PartType.Ball
OrbTemplate.Material = Enum.Material.Neon
OrbTemplate.Size = Vector3.new(0.6,0.6,0.6)
OrbTemplate.Anchored = true
OrbTemplate.CanCollide = false
local function createWaypoint(position)
local orb = OrbTemplate:Clone()
orb.Position = position + Vector3.new(0,2,0)
orb.Parent = OrbFolder
return orb
end
-- set up some navigation variables and set up a chain
-- so that when the humanoid arrives at a waypoint,
-- it automatically selects the next point.
local currentOrbIndex = 0
local currentPath = {} -- store pathfinding waypoints here
local function moveToNextWaypoint(previousPointReached)
currentOrbIndex += 1
if currentOrbIndex > #currentPath then
return
end
local nextWaypoint = currentPath[currentOrbIndex]
humanoid:MoveTo(nextWaypoint.Position)
end
humanoid.MoveToFinished:Connect(moveToNextWaypoint)
-- calculate and continually update the path
local pauseLength = 1.0 --seconds (lower this number to speed up refresh rate)
while wait(pauseLength) do
-- find the closest player's character model
local targetCharacter = nil
local closestDist = math.huge
local playerList = PlayerService:GetPlayers()
for i, player in pairs(playerList) do
-- check that the player's character has spawned in
local character = player.Character
if not character then
continue
end
-- do some quick maths and select which player is closest
local p1 = character.PrimaryPart.Position
local p2 = rootPart.Position
local d = math.abs((p1 - p2).Magnitude)
if d < closestDist then
closestDist = d
targetCharacter = character
end
end
-- check that there are actually players in the server
if targetCharacter == nil then
continue
end
-- compute the path to the character
local characterPos = targetCharacter.PrimaryPart.Position
local path = PathfindingService:CreatePath()
path:ComputeAsync(rootPart.Position, characterPos)
local waypoints = path:GetWaypoints()
-- debug : show the path to the player
if shouldDebugPath then
OrbFolder:ClearAllChildren()
for i, waypoint in pairs(waypoints) do
local position = waypoint.Position
createWaypoint(position)
end
end
-- start the player walking towards the nearest player
currentOrbIndex = 0
currentPath = waypoints
moveToNextWaypoint()
end
This solution recalculates the path to the nearest player every 1 second. That path is stored as a series of waypoints and it uses the Humanoid.MoveToFinished signal as the trigger to move to the next known waypoint. Whenever the path is recalculated, we simply start the process over.
With this system, the loop isn't blocked and the distance and path to the different players can be regularly recalculated. This allows the NPC to dynamically switch targets towards the closest player too.

Related

Equation script for scaling rebirth cost not working

So basically I'm making a roblox clicker game where I have rebirths which have a cost and i made a menuOrganiser script that basically automatically makes rebirth buttons every time the value of players.upgrades.rebirthbuttons changes and I have added an equation that should scale the cost every time I rebirth but for someone it doesn't change the cost on the actual screen it says that the cost is 800 but when you try to rebirth with 800 clicks it doesn't work so it changes it but not the GUI and I'm wondering why.
This is the script with the equation for the rebirths. And I also get this error:
Players.kukata4321.PlayerGui.ScreenGui.RebirthMenu.ScrollingFrame.menuOrganiser:55: attempt to index number with 'Value'
...
rebirthButtons.Changed:Connect(function()
local amount = rebirthButtons.Value
for count = 1, amount do
if not scrollingframe:FindFirstChild(rebirthOptions[count]) then
local newButton = template:Clone()
local rebirthsAmount = rebirthOptions[count]
newButton.Name = rebirthsAmount
newButton.rebirthAmount.Text = rebirthsAmount.." Rebirths"
newButton.cost.Text = rebirthsAmount*800*((1)+((rebirths.Value)/10)).. " Clicks"
newButton.Parent = scrollingframe
end
end
end)
rebirths.Changed:Connect(function()
local rebirths = leaderstats:WaitForChild("Rebirths").Value
for _, child in pairs(scrollingframe:GetChildren()) do
if child:IsA("TextButton") then
local rebirthsAmount = tonumber(child.Name)
child.cost.Text = rebirthsAmount*800*((1)+((rebirths.Value)/10)).. " Clicks"
end
end
end)
So what your error means is that you defined a variable with an IntValue's value (which is a number) and then tried to get the "Value" of a number
Eg:
local rebirths = game.Players.leaderstats.Rebirths.Value -- returns a number
print(rebirths.Value) -- 12345.Value doesn't work
The simple solution is to remove .Value from when you define the variable
rebirths.Changed:Connect(function()
local rebirths = leaderstats:WaitForChild("Rebirths") -- Remove .Value
for _, child in pairs(scrollingframe:GetChildren()) do
if child:IsA("TextButton") then
local rebirthsAmount = tonumber(child.Name)
child.cost.Text = rebirthsAmount*800*((1)+((rebirths.Value)/10)).. " Clicks"
end
end

How to aim a gun where the camera is pointing?

I have a fixed "Over-The-Shoulder" camera system, and i want to aim the gun where the camera is pointing at.
My best bet was to use the CFrame.Rotation, but it didn't work so well.
That's when i tried, getting the angles from the camera every time it changed, using: workspace.CurrentCamera.Change:Connect, and then applying the rotation vectors (Camera.CFrame.Rotation) to the entire of the player's body.
Then i've tried implementing Inverse Kinematics, to my code, but honestly, i could not stand copied code that i don't know how it works, so i scraped everything that i've copied.
Everything that i found about IK (Inverse Kinematics), was little to no useful, since no one really did what i want to achieve.
I don't know if it's achievable or not, but, if it helps, here is the state of my code, and an example image.
-- Script for controling the player camera.
-- Used for aiming in general.
-- Game global table
local globals = require(game.ReplicatedStorage.globalStates)
-- Normal services
local UserInputService, runService = game:GetService("UserInputService"), game:GetService("RunService")
local LocalPlayer = game:GetService("Players").LocalPlayer -- Get client's player
local Camera = game:GetService("Workspace").CurrentCamera -- Player's camera(?)
local TweenService = game:GetService("TweenService")
local PlayerMouse = LocalPlayer:GetMouse()
local DEF_FOV = 70
local AIM_FOV = 35
local AIM_SENS = 0.3
local AIM_DURR_TIME = 0.15
local xAngle, yAngle = 0, 0
local cameraOffset = Vector3.new(3.5, 0, 7.5)
local tweenIn = TweenService:Create(Camera, TweenInfo.new(AIM_DURR_TIME), {FieldOfView = AIM_FOV})
local tweenOut = TweenService:Create(Camera, TweenInfo.new(AIM_DURR_TIME), {FieldOfView = DEF_FOV})
-- TODO: Fix bug when player changes camera mode in menu
wait(1)
Camera.CameraType = Enum.CameraType.Scriptable -- Used for creating custom behavior
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter -- Lock the cursor
PlayerMouse.Icon = "rbxassetid://9095360160" -- Set dot crosshair
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
xAngle = xAngle - input.Delta.x * 0.4
yAngle = math.clamp(yAngle - input.Delta.y * 0.4, -80, 80)
end
end)
-- This will run every frame
runService.RenderStepped:Connect(function()
local Character = LocalPlayer.Character
local Root = LocalPlayer.Character:FindFirstChild("HumanoidRootPart")
if Character and Root then
local startCFrame = CFrame.new((Root.CFrame.p + Vector3.new(0,2,0))) * CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0)
local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraOffset.X,cameraOffset.Y,cameraOffset.Z))
local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraOffset.X,cameraOffset.Y,-50000))
Camera.CFrame = CFrame.new(cameraCFrame.p ,cameraFocus.p)
end
end)
-- Only 'aim' if TOGGLE_MOUSE and TOGGLE_MOUSE are false
PlayerMouse.Button2Down:Connect(function()
if globals.TOGGLE_MOUSE == false and globals.IS_PLAYER_AIMING == false then
tweenIn:Play()
globals.IS_PLAYER_AIMING = true
else
print("Could not start player aim")
end
end)
PlayerMouse.Button2Up:Connect(function()
tweenOut:Play()
globals.IS_PLAYER_AIMING = false
end)

How to make NPC in roblox walk around normally then when it sees a player it runs after them

The titles explains it all, I'm trying to make a roblox npc walk around the maze normally, then when it sees the player, it starts running after the player to kill it, I have the NPC, I have the killing part, I just need the code for the NPC walking around normally and the code for the NPC detecting the player then running after them. Thanks! :D
edited: heres my code
debugMode = false
targetNPCs = false
--
h = script.Parent.Parent:WaitForChild("Humanoid")
pathService = game:GetService("PathfindingService")
targetV = script.Parent:WaitForChild("Target")
function closestTargetAndPath()
local humanoids = {}
if targetNPCs then
local function recurse(o)
for _,obj in pairs(o:GetChildren()) do
if obj:IsA("Model") then
if obj:findFirstChild("Humanoid") and obj:findFirstChild("Torso") and obj.Humanoid ~= h and obj.Humanoid.Health > 0 and not obj:findFirstChild("ForceField") then
table.insert(humanoids,obj.Humanoid)
end
end
recurse(obj)
end
end
recurse(workspace)
else
for _,v in pairs(game.Players:GetPlayers()) do
if v.Character and v.Character:findFirstChild("HumanoidRootPart") and v.Character:findFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 and not v:findFirstChild("ForceField") then
table.insert(humanoids,v.Character.Humanoid)
end
end
end
local closest,path,dist
for _,humanoid in pairs(humanoids) do
local myPath = pathService:ComputeRawPathAsync(h.Torso.Position,humanoid.Torso.Position,500)
if myPath.Status ~= Enum.PathStatus.FailFinishNotEmpty then
-- Now that we have a successful path, we need to figure out how far we need to actually travel to reach this point.
local myDist = 0
local previous = h.Torso.Position
for _,point in pairs(myPath:GetPointCoordinates()) do
myDist = myDist + (point-previous).magnitude
previous = point
end
if not dist or myDist < dist then -- if true, this is the closest path so far.
closest = humanoid
path = myPath
dist = myDist
end
end
end
return closest,path
end
function goToPos(loc)
h:MoveTo(loc)
local distance = (loc-h.Torso.Position).magnitude
local start = tick()
while distance > 4 do
if tick()-start > distance/h.WalkSpeed then -- Something may have gone wrong. Just break.
break
end
distance = (loc-h.Torso.Position).magnitude
wait()
end
end
while wait() do
local target,path = closestTargetAndPath()
local didBreak = false
local targetStart
if target and h.Torso then
targetV.Value = target
targetStart = target.Torso.Position
roaming = false
local previous = h.Torso.Position
local points = path:GetPointCoordinates()
local s = #points > 1 and 2 or 1
for i = s,#points do
local point = points[i]
if didBreak then
break
end
if target and target.Torso and target.Health > 0 then
if (target.Torso.Position-targetStart).magnitude < 1.5 then
local pos = previous:lerp(point,.5)
local moveDir = ((pos - h.Torso.Position).unit * 2)
goToPos(previous:lerp(point,.5))
previous = point
end
else
didBreak = true
break
end
end
else
targetV.Value = nil
end
if not didBreak and targetStart then
goToPos(targetStart)
end
end
I won't provide any code as you have not shown any efforts to solve that problem.
Enter "roblox npc" into www.google.com. Click the second hit:
https://developer.roblox.com/en-us/articles/Moving-NPCs-Between-Points
This covers the walking around part and also links this
https://developer.roblox.com/en-us/articles/Pathfinding
which covers more complex movements.
The seeing part is done with ray casting
https://developer.roblox.com/en-us/articles/Raycasting
If you can cast a ray from the NPC to your player calculate a path to that player and attack.

Attempt to index nil with "Touched"?

Here's the deal, I'm incorporating a teleportation system with a tool on Roblox Studio. However, this error has got me stumped. I will provide the code and other useful sources below and describe the issue after.
local tool = script.Parent.Parent
local debounce = false
local afterTeleport = false
local plrName = game.Players.LocalPlayer.Name
local char = workspace:FindFirstChild(plrName)
tool.Activated:connect(function(m)
if debounce == false then
local hum = game.Players.LocalPlayer.Character.Humanoid
local anim_feet = hum:LoadAnimation(script.Parent.Animation)
local current = anim_feet
local bodyVelocity = tool.Handle.LocalScript.BodyVelocity:Clone()
debounce = true
current:Play()
wait(1.1)
local gui = script.Parent.TransitionGui:Clone()
-- Properties for UI
gui.Parent = game.Players.LocalPlayer.PlayerGui
-- Transition
gui.Frame.BackgroundTransparency = 1
wait(0.02)
gui.Frame.BackgroundTransparency = 0.8
wait(0.02)
gui.Frame.BackgroundTransparency = 0.6
wait(0.02)
gui.Frame.BackgroundTransparency = 0.4
wait(0.02)
gui.Frame.BackgroundTransparency = 0.2
wait(0.02)
gui.Frame.BackgroundTransparency = 0
-- Teleport Player Into Sky Above Them
hum.Parent.HumanoidRootPart.CFrame =
CFrame.new(Vector3.new(hum.Parent.HumanoidRootPart.CFrame.X, hum.Parent.HumanoidRootPart.CFrame.Y + 700, hum.Parent.HumanoidRootPart.CFrame.Z))
bodyVelocity.Parent = hum.Parent.HumanoidRootPart
afterTeleport = true
wait(0.02)
gui.Frame.BackgroundTransparency = 0.2
wait(0.02)
gui.Frame.BackgroundTransparency = 0.4
wait(0.02)
gui.Frame.BackgroundTransparency = 0.6
wait(0.02)
gui.Frame.BackgroundTransparency = 0.8
wait(0.02)
gui.Frame.BackgroundTransparency = 1
wait(0.02)
end
end)
char.Touched:Connect(function(interact)
local hum = game.Players.LocalPlayer.Character.Humanoid
if afterTeleport == true then
if interact:IsA("Terrain") then
if hum.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hum.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
elseif interact:IsA("Part") then
if hum.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hum.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
elseif interact:IsA("MeshPart") then
if hum.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hum.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
end
end
end)
To me, this seems correct. But the issue is whenever I play-test the code, the output displays an error that I don't understand. The output error is:
16:35:59.453 Players.BigBetterBuilder.Backpack.Sky Port.Handle.LocalScript:58: attempt to index nil with 'Touched' 
My goal for this tool is that when it is activated, it will teleport you into the sky 700 studs above the players' current position. It will add a BodyVelocity to the player's HumanoidRootPart causing the player to slow down the decent speed, and when the player touched the ground. It should remove the BodyVelocity allowing the player to roam around without having weird gravity.
But the function that's supposed to detect when a player touches the ground isn't working. And I, unfortunately, can't seem to solve the problem.
You've got a timing issue in your code. At the top of your script, you are trying to access the player's character model, but when this script runs, that character might not have been loaded into the workspace yet. So, when you call char.Touched:Connect(...) it throws an error because char is null.
But you've got a different problem as well. The player's character is a Model, and Model's don't have a Touched event like Parts do. So in order to use the Touched event, you either need to attach it to platforms that a character might touch, or to the Parts inside the character model.
So try moving the char.Touched connection inside a callback that fires once the player and character have properly loaded into the game, and attach the Touched connection instead to the different Parts in the character model :
local player = game.Players.LocalPlayer
player.CharacterAdded:Connect(function(character)
-- create a helper function for checking if we're touching the ground
local function checkTouchedGround(interact)
if not afterTeleport then
return
end
local shouldRemoveVelocity = false
if interact:IsA("Terrain") then
shouldRemoveVelocity = true
elseif interact:IsA("Part") then
shouldRemoveVelocity = true
elseif interact:IsA("MeshPart") then
shouldRemoveVelocity = true
end
if shouldRemoveVelocity then
local bv = character.HumanoidRootPart:FindFirstChild("BodyVelocity", 0.1)
if bv then
bv:Destroy()
end
end
end
-- listen for any time any player parts touch the ground
for _, child in ipairs(character:GetDescendants()) do
if child:isA("BasePart") then
child.Touched:Connect(checkTouchedGround)
end
end
end)

How would I make players on teams detect parts of a player?

I am using WorldToScreenPoint and GetPartsObscuringTarget. What I am trying to fix is that a player must detect parts of player on a different team. For example, there is a player on a team alone trying to find other players by detecting one of their humanoid parts. The script only detect parts inside the Workspace.
I've tried using Player.CharacterAdded:Wait() and a nested loop to go through players if they are on the same team or not. This didn't worked. I've been trying to fix this for the past 2 weeks.
local Player = game.Players.LocalPlayer
local Character = workspace:FindFirstChild(Player.Name)
game:GetService("RunService").Heartbeat:Connect(function()
for _, object in pairs(Character:GetDescendants()) do
if object:IsA("Part") or object:IsA("MeshPart") then
if not object:IsDescendantOf(game.Players.LocalPlayer.Character) then
local object2,visible = workspace.CurrentCamera:WorldToScreenPoint(object.Position)
if visible == true then
local pos1 = workspace.CurrentCamera.CFrame.Position
local pos2 = object.Position
local castPoints = {pos1,pos2}
local ignoreList = {game.Players.LocalPlayer.Character, object}
local obstructs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
if obstructs[1] then
return
elseif not obstructs[1] then
print(object.Name.." has been detected!")
end
elseif visible ~= true then
print("You are now incognito.")
return
end
end
end
end
end)

Resources