繁体   English   中英

尝试使用 characterAdded 索引 nil

[英]Attempt to index nil with characterAdded

我需要为一个系统获取玩家的角色,如果触摸某个部分会造成损坏,但这是由普通脚本而不是本地脚本发生的,我想防止角色受到伤害。 我怎样才能做到这一点?

我试图制作这段代码,但没有奏效。

for i, v in pairs(script.Parent:GetChildren()) do
    if v:IsA("MeshPart") then
        v.Touched:Connect(function(p)
            local db = true
            local char = game.Players:GetPlayerFromCharacter(p.Parent).CharacterAdded:Wait()
            if char then
                if p.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db == true then
                    db = false
                    p.Parent.Humanoid.Health -= 150
                    task.wait(3)
                    db = true
                end
            end
        end)
    end
end

您应该将触摸的事件连接到击杀砖,而不是玩家的肢体,您还需要这样做=-不是-=

local killBrick = script.Parent

killBrick.Touched:Connect(function(p)
    local db = true
    if p.Parent:FindFirstChildOfClass("Humanoid") then -- make sure the thing its touching is a player/has a humanoid
        if db == true then
            db = false
            p.Parent:FindFirstChildOfClass("Humanoid").Health =- 150
            task.wait(3)
            db = true
        end
    end
end)

还要确保这是一个服务器脚本,并且它的父级是杀死砖如果你想保护某人免受这种情况的影响,只需执行p.Parent.Name == "Username"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM