标记一个接触脚本的玩家。父母

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

所以我想用它来标记玩家,但它什么都不做!

local playersTouching = {}

local function onPartTouched(part, player)
    if part == script.Parent and player and player:IsA("Player") then
        player:SetAttribute("Hiding", true)
        playersTouching[player] = true
    end
end

local function onPartTouchEnded(part, player)
    if part == script.Parent and player and player:IsA("Player") then
        player:SetAttribute("Hiding", nil)
        playersTouching[player] = nil
    end
end

script.Parent.Touched:Connect(function(otherPart)
    local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid then
        onPartTouched(script.Parent, humanoid.Parent)
    end
end)

script.Parent.TouchEnded:Connect(function(otherPart)
    local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid then
        onPartTouchEnded(script.Parent, humanoid.Parent)
    end
end)

所以我在 Roblox 上执行了它,Player 触摸了 script.Parent 并且没有被标记。这样做的另一个目的是检测玩家何时触摸某个部分,而另一个带有脚本的部分触摸了他们。如果我的方法不起作用,请给我另一种方法。谢谢!

AMMEDNMENT:现在我的检测脚本无法正常工作大声笑所以我得到的是 char 而不是 player 是同一个问题吗:

script.Parent.Touched:Connect(function(player)
    print("TOUCHED")
    if player:GetAttribute("Hiding") then
        print("tag player touch????")
    else
        print("HMM")

    end
end)
lua roblox
1个回答
0
投票

您犯了一个简单的错误,将 Player 对象与其在工作区周围运行的 character 模型 混淆了。他们都有相同的名字,但属于不同的类别。

看着这些线:

onPartTouched(script.Parent, humanoid.Parent)

-- which calls ... 

local function onPartTouched(part, player)
    if part == script.Parent and player and player:IsA("Player") then

您正在检查

player
变量是否为
IsA("Player")
,但此检查将始终为假,因为
humanoid.Parent
是模型,而不是玩家。

获取玩家的更好方法是使用Players:GetPlayerFromCharacter()。当涉及到

Touched
TouchEnded
信号时,需要注意的是您的角色模型可以多次发射信号。因此,为确保代码仅在您完全停止触摸部件时触发,最好记录有多少部件或触摸了多少块。查看此answer了解更多详情。

所以试试这个:

local Players = game:GetService("Players")
local playersTouching = {} -- <Player, int>

script.Parent.Touched:Connect(function(otherPart)
    local player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if player then
        -- only set the attribute once
        if not playersTouching[player] then
            playersTouching[player] = 0

            -- do the thing for the first time
            player:SetAttribute("Hiding", true)
            print(player.Name .. " is now hiding")
        end

        -- keep count of the number of objects touching the block
        playersTouching[player] += 1
    end
end)

script.Parent.TouchEnded:Connect(function(otherPart)
    local player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if player then
        -- decrement the counter
        playersTouching[player] -= 1

        -- only remove the attribute once all the touching pieces have stopped
        if playersTouching[player] == 0 then
            playersTouching[player] = nil
      
            -- do the thing that happens only once
            player:SetAttribute("Hiding", nil)
            print(player.Name .. " is no longer hiding")
        end
    end
end)
© www.soinside.com 2019 - 2024. All rights reserved.