Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Issue with NPC script?[SOLVED]

Asked by
Bman8765 270 Moderation Voter
9 years ago

Okay so I have an npc and when it touches you I want to it to damage the player if they are a certain type of player. Their player type is defined in a stringvalue in a ScreenGUI in their playergui named CharacterType. When the npc touches the player I want it to NOT damage the players with a character type of Controller but I do want it to damage someone with the character type of Survivor.

Here is the script (not the entire, thing, the entire script is below, what is here is the most important parts of the script):

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function Hit(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")
    local player = hit.Parent
    if human ~= nil then
        if game.Players[player.Name].PlayerGui.MainScreen.PlayerValues.CharacterType.Value == "Survivor" then
            human.Health =  human.Health -10
        end
    end
end

larm.Touched:connect(Hit)
rarm.Touched:connect(Hit)

Full code:

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

function Hit(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")
    local player = hit.Parent
    if human ~= nil then
        if game.Players[player.Name].PlayerGui.MainScreen.PlayerValues.CharacterType.Value == "Survivor" then
            human.Health =  human.Health -10
        end
    end
end

larm.Touched:connect(Hit)
rarm.Touched:connect(Hit)

while true do
    wait(0.1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Zombie:MoveTo(target.Position, target)
    end
end

0
There was a type-o in the script I found.. everything works now. Feel free to use the script if you want Bman8765 270 — 9y

Answer this question