How can I fix this problem with my Roblox AI Monster GUI?

59 views Asked by At

We have been working on a Roblox AI for our horror game but are encountering an error where when you are being chased it is not enabling the GUI for being chased.

We have tried to change the properties of multiple parts of the GUI including but not limited too, changing the Frame Visibility, Frame Enabled, Screen GUI enabled. However placing the frame.Visible = true (or false) in another part of the script leads to a success for some reason.

Here is the script for Reference:

--//SCRIPT MADE BY ZRYLX//--


--//Variables//--

--Pathfinding service
local pfs = game:GetService("PathfindingService")


--AI stuff
local AI = script.Parent
local humanoid = AI:WaitForChild("Humanoid")
local hrootpart = AI:WaitForChild("HumanoidRootPart")
hrootpart:SetNetworkOwner(nil)
local pathParamaters = {
    AgentHeight = 5,
    AgentRadius = 3,
    AgentCanJump = true,
}
humanoid.WalkSpeed = 16
local isChasing = false

--Raytracing stuff
local rayParamaters = RaycastParams.new()
rayParamaters.FilterType = Enum.RaycastFilterType.Exclude
rayParamaters.FilterDescendantsInstances = {AI}

--Stuff for chasing target
local lastPos
local maxSearchArea = 200

--UI
local thing = script.Parent.Parent.Parent.StarterGui.AIChasing.Frame

--//Functions//--

function makeVisible()
    if isChasing == true then
        print("the ai is chasing...")
        local frame = script.Parent.Parent.Parent.StarterGui:FindFirstChild("AIChasing")
        if frame then
            frame.Enabled = true
            print("Made visible")
        else
            warn("Frame or AIChasing not found")
        end
    else
        print("the ai is not chasing...")
        local frame = script.Parent.Parent.Parent.StarterGui:FindFirstChild("AIChasing")
        if frame then
            frame.Enabled = false
            print("Made invisible")
        else
            warn("Frame or AIChasing not found")
        end
    end
end

--Attacking the target
function attack(target)
    local dist = (hrootpart.Position - target.HumanoidRootPart.Position).Magnitude
    local deb = false

    if dist > 3 then
        humanoid:MoveTo(target.HumanoidRootPart.Position)
    else
        if deb == false then
            deb = true
            target.Humanoid.Health = 0
            humanoid.WalkSpeed = 16
            isChasing = false
            print("Making the frame not visible")
            script.Parent.Parent.Parent.StarterGui.AIChasing.Enabled = false
            print("Made not visible")
            task.wait(0.5)
            deb = false
        end
    end
end

--Checks if the ai can actually see the target with raytracing
function canSee(target)
    local dir = (target.HumanoidRootPart.Position - hrootpart.Position).Unit * maxSearchArea
    local orgin = hrootpart.Position
    local ray = workspace:Raycast(orgin, dir, rayParamaters)

    if ray and ray.Instance then
        if ray.Instance:IsDescendantOf(target) then
            return true
        else
            return false
        end
    else
        return false
    end
end

--Searches area for target
function searchforTarget()
    local plrs = game.Players:GetPlayers()
    local maxDist = maxSearchArea
    local closestTarget

    for i, player in pairs(plrs) do
        if player.Character then
            local target = player.Character
            local dist = (hrootpart.Position - target.HumanoidRootPart.Position).Magnitude

            if dist < maxDist and canSee(target) then
                closestTarget = target
                maxDist = dist
            end
        end
    end

    return closestTarget
end

--Get the path with pathfinding
function getPath(destination)
    local path = pfs:CreatePath(pathParamaters)
    path:ComputeAsync(hrootpart.Position, destination.Position)
    return path 
end

--The main part - if there is a target, chase the target. Else, patrol the area 
function followpathTo(destination)
    local path = getPath(destination)

    if path.Status == Enum.PathStatus.Success then
        for i, waypoint in pairs(path:GetWaypoints()) do
            path.Blocked:Connect(function()
                path:Destroy()
            end)
            local target = searchforTarget()

            if target and target.Humanoid.Health > 0 then
                isChasing = true
                humanoid.WalkSpeed = 20
                lastPos = target.HumanoidRootPart.Position
                attack(target)
                break
            else
                if waypoint.Action == Enum.PathWaypointAction.Jump then
                    humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
                end

                if lastPos then
                    humanoid:MoveTo(lastPos)
                    humanoid.MoveToFinished:Wait()
                    lastPos = nil
                    break
                else
                    humanoid:MoveTo(waypoint.Position)
                    humanoid.MoveToFinished:Wait()
                end
            end
        end
    else
        return
    end
end


--//Main loop//--
while task.wait do
    local waypoints = workspace.Waypoints:GetChildren()
    local randomNum = math.random(1, #waypoints)
    followpathTo(waypoints[randomNum])
    makeVisible()
end
1

There are 1 answers

0
EliTheGingerCat On

The Script sets whether the ScreenGui in StarterGui is enabled, rather than for any specific player. Try something like:

-- assuming target is the Player who is being chased
target.PlayerGui.AIChasing.Enabled = true