简体   繁体   中英

Attempt to index nil with characterAdded

I need to get the character of the player for a system thats give a damage if a part is touched but this occur by a normal script and not a local script and i want to prevent the character take damage. How i can make this?

I tried to make this code but didn't worked.

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

You should connect the touched event to the kill brick, not the players limbs and you also need to do =- not -=

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)

Also make sure this is a server script and that its parented to the kill brick If you want to protect someone from this just do p.Parent.Name == "Username"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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