Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What do I do wrong with the functions and events?

Asked by
brok4d 77
5 years ago

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)
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. User#22604 1 — 5y
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. gullet 471 — 5y
0
Thank you brok4d 77 — 5y
0
Then when it leaves the disconnection will it automatically connect? brok4d 77 — 5y
0
And this kind of script where do you have to go? brok4d 77 — 5y

1 answer

Log in to vote
0
Answered by
brok4d 77
5 years ago

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)
Ad

Answer this question