测距仪不打印文本

Distance finder not printing text

我正在尝试制作这个标志,当你走得足够近时,会显示一个 GUI。

为了测试它,我做了它 print("It works!") 但它不会打印任何东西。

本地脚本:

local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer

for _,Player in next, Players:GetChildren() do
    local character = Player.Character
    if character and character.Parent and Player ~= LocalPlayer then
        local Magnitude = (LocalPlayer.Character.HumanoidRootPart.Position - character.HumanoidRootPart.Position).magnitude
        
    end
end
while true do
    if Magnitude < 10 then
        print("It's working!")
    end
end

问题是您已将 LocalScript 放入工作区的部件中。如果您检查 the LocalScript docs,您会看到 ...

a LocalScript will only run Lua code if it is a descendant of one of the following objects:

  • A Player’s Backpack, such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts.
  • The ReplicatedFirst service

因此,要解决您的问题,您需要

  • A) 将您的代码转换为脚本并通过播放器服务访问播放器对象。或
  • B) 将 LocalScript 移动到它将开始执行的位置并相应地更新代码。

如果您走选项 B 的路线,请按照以下步骤操作:

首先,将 LocalScript 移动到 StarterPlayer > StarterCharacterScripts。这将导致脚本在玩家角色重生到世界中时执行。

然后,更新代码,使其能够找到方块并向玩家显示距离。

local localPlayer = game.Players.LocalPlayer

-- locate the sign in the world
-- local signGui = game.Workspace:WaitForChild("Part"):WaitForChild("UI")

-- set the range where the sign should appear
local DISTANCE_TO_SHOW_SIGN = 10 -- studs

-- every game tick, calculate the distance between the part and the character
game.RunService.Heartbeat:Connect(function()
    -- escape if the player's character doesn't exist
    if not localPlayer.Character then
        return
    end

    -- calculate the distance to each of the players
    -- if two players are close enough to each other, show the sign
    local shouldShowSign = false
    local myPosition = localPlayer.Character.HumanoidRootPart.Position

    for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
        if (otherPlayer ~= localPlayer) and (otherPlayer.Character) then
            local theirPosition = otherPlayer.Character.HumanoidRootPart.Position
            local distBetween = (myPosition - theirPosition)
            
            -- optimization : when calculating distance, try to avoid using square roots
            -- use pythagorean theorem : dist = sqrt(x^2 + y^2 + z^2)
            -- instead, use dist^2 = x^2 + y^2 + z^2
            if (distBetween * distBetween) < (DISTANCE_TO_SHOW_SIGN ^ 2) then
                shouldShowSign = true
                break
            end
        end
    end

    -- show the sign if two players are close enough together
    if shouldShowSign then
        print("it's working")
    end
    -- optimization : only set the visibility when it changes
    -- if signGui.Visible ~= shouldShowSign then
    --    signGui.Visible = shouldShowSign
    -- end
end)

您的代码有 3 个主要问题 1:是楷书,不是方文字 2:你的 while true do 循环会在服务器启动时超时 3:仅在执行 for i,v

时才定义幅度

改为在本地脚本中尝试此代码

local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer
local Magnitude
while true do
    wait()
    for _,Player in next, Players:GetChildren() do
    local character = Player.Character
    if character and character.Parent and Player ~= LocalPlayer then
        Magnitude = (LocalPlayer.Character.HumanoidRootPart.Position - character.HumanoidRootPart.Position).magnitude
        
    end
end

if Magnitude ~= nil then
    if Magnitude < 10 then
        print("It's working!")
    end
end
end