Hello, I have a problem I get this error on line 18 "Connected can not be assigned to" what I'm trying to do is that when the npc is made to the player, change the status and disconnect the RemoteEvent. What am I doing wrong?it is a nomal script and it is inside the npc Thank you.
local PatrolWalkPause = game.ReplicatedStorage.PatrolWalkPause local Players = game:GetService("Players") local PathfindingService = game:GetService("PathfindingService") local enemy = script.Parent local humanoid = enemy.Humanoid local humanoidRootPart = enemy.HumanoidRootPart local pointA = script.Parent.Parent.Start local pointB = script.Parent.Parent.Finish local currentDestination = pointA Players.PlayerAdded:Connect(function(player) if player.Character ~= nil then while true do local mag = (enemy.Head.Position - player.Character.PrimaryPart.Position).magnitude print(mag) if mag <= 50 then conecction:Disconnect() else conecction.Connected = true end wait() end end end) conecction = PatrolWalkPause.OnServerEvent:Connect(function(patrol) while wait(2) do local startingPosition = humanoidRootPart.Position local goalPosition = currentDestination.Position local path = PathfindingService:FindPathAsync(startingPosition, goalPosition) local waypoints = path:GetWaypoints() for waypointIndex, waypoint in pairs(waypoints) do local waypointPosition = waypoint.Position humanoid:MoveTo(waypointPosition) humanoid.MoveToFinished:Wait() end if currentDestination == pointA then currentDestination = pointB else currentDestination = pointA end end end)
I have modified the script and now what does not work at all or sometimes works but only half calculates the distance while the loop does not work well.
local Players = game:GetService("Players") local PathfindingService = game:GetService("PathfindingService") local enemy = script.Parent local humanoid = enemy.Humanoid local humanoidRootPart = enemy.HumanoidRootPart local pointA = script.Parent.Parent.Start local pointB = script.Parent.Parent.Finish local currentDestination = pointA function PatrolWalk() while wait(2) do local startingPosition = humanoidRootPart.Position local goalPosition = currentDestination.Position local path = PathfindingService:FindPathAsync(startingPosition, goalPosition) local waypoints = path:GetWaypoints() for waypointIndex, waypoint in pairs(waypoints) do local waypointPosition = waypoint.Position humanoid:MoveTo(waypointPosition) humanoid.MoveToFinished:Wait() end if currentDestination == pointA then currentDestination = pointB else currentDestination = pointA end end end local function onPlayerAdded(player) if player ~= nil and player.Character ~= nil then while true do local mag = (enemy.Head.Position - player.Character.Head.Position).magnitude if mag < 50 then print("Attack") else print(mag) PatrolWalk() end wait() end end end Players.PlayerAdded:Connect(onPlayerAdded)