我们一直在为我们的恐怖游戏开发 Roblox AI,但遇到了一个错误,即当你被追赶时,它不会启用 GUI 来被追赶。
我们尝试更改 GUI 多个部分的属性,包括但不限于更改框架可见性、框架启用、屏幕 GUI 启用。然而,由于某种原因,将frame.Visible = true(或false)放在脚本的另一部分会导致成功。
这是参考脚本:
--//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
脚本设置是否启用StarterGui中的ScreenGui,而不是针对任何特定播放器。尝试这样的事情:
-- assuming target is the Player who is being chased
target.PlayerGui.AIChasing.Enabled = true