I have to make electricity fence for my game which kill every npc touching it. Im newbie to the roblox scripting, and every killbrick i make doesnt harm NPC's, but players.
Can anyone tell me how do i make this script? Or maybe even write it, if you want?
thanks.
I'll try to give you an idea of how to go about this.
You can try adding a BoolValue named "isPlayer", or you can name it whatever you want into StarterCharacterScripts (You can insert a StringValue into Workspace first then reparent it into StarterCharacterScripts), make a regular script checking if the character that hits the fence has that value or not. If they don't have it, then they will be identified as an NPC, then you can kill it.
It would look like something like this:
script.Parent.Touched:Connect(function(hit) local human = hit.Parent.Humanoid if not hit.Parent:FindFirstChild(valuename) then -- If it didn't found the val -- rest of the code goes here end end)
First of all, name all of your NPC's humanoids to something other than 'Humanoid'.
Now, make a normal kill script but instead of checking if it is a player, check if the Humanoid in the player exists + is called whatever you called your NPCs' humanoids.
Example: Let's say all your NPC's humanoids were called 'NPCBot'.
For the kill brick script if it was inside the brick, you would do the following:
local killbrick = script.Parent -- Get the killbrick killbrick.Touched:Connect(function(hit) -- See if it gets touched and connect a function local hum = hit.Parent:FindFirstChild('NPCBot') -- Check to see if it is a NPC if hum then -- If it is a NPC then hum.Health = 0 -- Set the humanoid's health to 0, killing it end end)
Hope this helps!
--add a BoolValue with False / True (does not matter) named identify to your NPC. script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("identify") and hit.Parent:FindFirstChild("Humanoid") then --Assuming all the parts are directly under the NPC. If not then put it into a while loop here, put the result in a variable and ascend keep its parent until it gets the “game.Workspace" parent or the NPC. hit.Parent.Humanoid.Health = 0 --this kills the NPC immediately, if you want to let him take damage then you can use the TakeDamage method. end end)
Rename the NPC's Humanoid so that it will only damage that kind of humanoid. For example:
Normal Humanoid - Name of the Humanoid is "Humanoid" and the script sees the name Humanoid and runs the script
Renamed Humanoid - Name of the Humanoid is "EnemyHumanoid" and the script will only run the script if it detects the name "EnemyHumanoid"
To detect whether the given character isn't a player's character, you would just put this in a script:
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChildOfClass("Humanoid")then --checking if the thing touching the brick is a character if not game.Players:GetPlayerFromCharacter(hit.Parent)then --does the character belong to a player? local Humanoid = script.Parent:FindFirstChildOfClass("Humanoid") Humanoid.Health = 0 end end end)
You don't even need to change the humanoid's name!