Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What do I do wrong with the functions and events?

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)

5 comments

0

You're defining "conecction" after you do functions on it, that doesn't work. You'd need to define conecction above the PlayerAdded function. Also, you can't set "conecction.Connected" since it's not a property of a RBXScriptConnection.

0

What are you trying to do here: [conecction.Connected = true] If you don't want to disconnect the connection you don't have to do anything. As the errors states you can't assign to Connected, it's a read only value.

0

Thank you

0

Then when it leaves the disconnection will it automatically connect?

0

And this kind of script where do you have to go?

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.