I’ve been working on my game nonstop lately and 1 thing i just can’t seem to get the hang of is making a part that can only kill npc but not players. Any help would be appreciated and if someone has time i could pay them to make it for me.
This should be in #help-and-feedback:scripting-support.
To answer your question, game.Players:GetPlayerFromCharacter() will return false if you try to put an argument about the NPC’s model, because NPCs are not actual players. Use that as your advantage for your if statement.
It not return false, it return nil if player does not exists
Yes, the person above this comment is also correct. Nil value is also a false value, use it in an if statement as your advantage.
Change Humanoid name then script : if zombie.CustomHumanoidName == nil then …
This code when placed inside a Script in Part
local killNPC = script.Parent
killNPC.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player == nil then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
end)
Very important distinction here that it’s falsy but not false. Nil and false are two distinct types, however both are considered “falsy”. For example if you have a conditional, both of those values will fail if you don’t explicitly check for a certain kind of value.
local something = nil
if something then -- Passes if something isn't false or nil
if something ~= nil then -- Passes even if it's false
if something ~= false then -- Passes if it's true or nil