我无法创建一个实体。 (罗布乐思)

问题描述 投票:0回答:1

好吧,这可能看起来很简单,但它不会停止尝试为角色索引 nil,或者它只是不会移动,或者它会移动,但不会杀死目标玩家(服务器中一次只允许一个。 )

在展示当前的代码之前,我将描述该实体是什么样的。该实体会在一段较长但随机的时间后在玩家周围生成。这种怪物可以穿越墙壁,所以逃离它的唯一方法就是跑得足够远或用手电筒照射它。这个实体有一个可定制的速度计数器,可以控制它的移动速度(显然)。我的主要代码一直有错误,所以我无法测试它。

这是我的代码。

local entity = script.Parent
 
local speed = 10

local player = game.Players.LocalPlayer 

local function chasePlayer()
  local humanoid = player.Character:FindFirstChild("Humanoid")

  if humanoid then
    local targetPosition = humanoid.RootPart.Position
    local direction = (targetPosition - entity.Position).unit

    entity.Velocity = direction * speed
  end
end

entity.Touched:Connect(function(otherPart)
  local character = otherPart.Parent
  local humanoid = character:FindFirstChild("Humanoid")

  if humanoid then
    humanoid.Health = 0
  end
end)

game:GetService("RunService").Heartbeat:Connect(chasePlayer)

我希望有人能纠正这个代码,因为我已经尝试了无数次让它工作但都失败了。

我尝试定义玩家,我希望它将该部分移向我的角色并杀死它,但即使没有错误,它也没有做任何事情。

lua roblox
1个回答
0
投票

如果您希望它仅针对所有客户端都可以看到移动的一个特定客户端,您将需要在完全不同的 LocalScript 中触发 RemoteEvent(这是受保护的,因为作弊者可以利用这一点,并进行检查,例如查看它们是否确实是那个)游戏真的试图让被追赶),因为如果这是一个 LocalScript,只有一个客户端会看到实体追赶他们,而其他客户端看到实体静止不动,我也看到你没有使用 Humanoid:MoveTo(vector3 ),而不是使用速度(应该是矢量3,但在你的情况下它显然是一个数字)。

这是我的代码版本,已修复。 请记住,只有客户才能看到追逐他们的实体,其他客户看不到。

如果实体不具备常规 Roblox 玩家角色的基本组件,就不可能(困难)制作实体。

    local entity = script.Parent

    local speed = 10

    local player = game.Players.LocalPlayer 

    local function chasePlayer()
      local humanoid = player.Character:FindFirstChild("Humanoid")
      if not entity then return end
      if humanoid then
         coroutine.wrap(function()
          local bool = true
          -- add parameters to make this value false when you want it to
             while bool == true do
            wait()
            -- I'm beginning to think this entity doesnt have a humanoid but lets assume it does and this is a part of it's body. 
            entity.Parent.Humanoid.WalkSpeed = speed
            if player.Character.Humanoid.RootPart then  
               entity.Parent.Humanoid:MoveTo(player.Character.Humanoid.RootPart.Position)
            end
         end
         end)()
      end
    end

    entity.Touched:Connect(function(otherPart)
      local character = otherPart.Parent
      local humanoid = character:FindFirstChild("Humanoid")

      if humanoid then
        humanoid.Health = 0
      end
    end)

    chasePlayer()

    -- game:GetService("RunService").Heartbeat:Connect(chasePlayer) -- ehh i would recommend removing this and replacing it with just chasePlayer() because i added a corountine.wrap() so it'll put pressure on the client and add lag.
© www.soinside.com 2019 - 2024. All rights reserved.