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

Why is my enemy attacking random npcs but not players?

Asked by 3 years ago
Edited by Ziffixture 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

Hello all, I am new to Roblox creating and programming. I have been using a lot of the pre-made models and stuff to kind of get in touch with the language and customizing them. I just started on the 11th and have learned so much already but am really new still. I have spent hours googling and searching for my issue. I have these 2 enemies that use the same "jeff the killer" script. The issue is they basically attack other bad guys or deer I have in the game. They completely ignore the player.

Can someone please point me in the right direct. I know this is prolly a stupid easy issue that I am just too dumb to see. Thanks for any help at all. The code main code for the enemy is here:

local JeffTheKillerScript = script
repeat
    Wait(0)
until JeffTheKillerScript and JeffTheKillerScript.Parent and JeffTheKillerScript.Parent.ClassName == "Model" and
    JeffTheKillerScript.Parent:FindFirstChild("Head") and
    JeffTheKillerScript.Parent:FindFirstChild("Torso")
local JeffTheKiller = JeffTheKillerScript.Parent
function raycast(Spos, vec, currentdist)
    local hit2, pos2 = game.Workspace:FindPartOnRay(Ray.new(Spos + (vec * .05), vec * currentdist), JeffTheKiller)
    if hit2 ~= nil and pos2 then
        if
            hit2.Name == "Handle" and not hit2.CanCollide or
                string.sub(hit2.Name, 1, 6) == "Effect" and not hit2.CanCollide
         then
            local currentdist = currentdist - (pos2 - Spos).magnitude
            return raycast(pos2, vec, currentdist)
        end
    end
    return hit2, pos2
end
function RayCast(Position, Direction, MaxDistance, IgnoreList)
    return Game:GetService("Workspace"):FindPartOnRayWithIgnoreList(
        Ray.new(Position, Direction.unit * (MaxDistance or 999.999)),
        IgnoreList
    )
end
 --
 --
 --
--[[if JeffTheKillerScript and JeffTheKiller and JeffTheKiller:FindFirstChild("Thumbnail")then]] --[[JeffTheKiller:FindFirstChild("Thumbnail"):Destroy();]] --[[end;]] local JeffTheKillerHumanoid
for _, Child in pairs(JeffTheKiller:GetChildren()) do
    if Child and Child.ClassName == "Humanoid" and Child.Health ~= 0 then
        JeffTheKillerHumanoid = Child
    end
end
local AttackDebounce = false
local JeffTheKillerKnife = JeffTheKiller:FindFirstChild("Knife")
local JeffTheKillerHead = JeffTheKiller:FindFirstChild("Head")
local JeffTheKillerHumanoidRootPart = JeffTheKiller:FindFirstChild("HumanoidRootPart")
local WalkDebounce = false
local Notice = true
local JeffLaughDebounce = false
local MusicDebounce = false
local NoticeDebounce = false
local ChosenMusic
function FindNearestBae()
    local NoticeDistance = 100
    local TargetMain
    for _, TargetModel in pairs(Game:GetService("Workspace"):GetChildren()) do
        if
            JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health ~= 0 and
                TargetModel.className == "Model" and
                TargetModel ~= JeffTheKiller and
                TargetModel.Name ~= JeffTheKiller.Name and
                TargetModel:FindFirstChild("Torso") and
                TargetModel:FindFirstChild("Head")
         then
            local TargetPart = TargetModel:FindFirstChild("Torso")
            local FoundHumanoid
            for _, Child in pairs(TargetModel:GetChildren()) do
                if Child and Child.ClassName == "Humanoid" and Child.Health ~= 0 then
                    FoundHumanoid = Child
                end
            end
            if
                TargetModel and TargetPart and FoundHumanoid and FoundHumanoid.Health ~= 0 and
                    (TargetPart.Position - JeffTheKillerHumanoidRootPart.Position).magnitude < NoticeDistance
             then
                TargetMain = TargetPart
                NoticeDistance = (TargetPart.Position - JeffTheKillerHumanoidRootPart.Position).magnitude
                local hit, pos =
                    raycast(
                    JeffTheKillerHumanoidRootPart.Position,
                    (TargetPart.Position - JeffTheKillerHumanoidRootPart.Position).unit,
                    500
                )
                if
                    hit and hit.Parent and hit.Parent.ClassName == "Model" and hit.Parent:FindFirstChild("Torso") and
                        hit.Parent:FindFirstChild("Head")
                 then
                    if
                        TargetModel and TargetPart and FoundHumanoid and FoundHumanoid.Health ~= 0 and
                            (TargetPart.Position - JeffTheKillerHumanoidRootPart.Position).magnitude < 9 and
                            not AttackDebounce
                     then
                        Spawn(
                            function()
                                AttackDebounce = true
                                local SwingAnimation =
                                    JeffTheKillerHumanoid:LoadAnimation(JeffTheKiller:FindFirstChild("Swing"))
                                local SwingChoice = math.random(1, 2)
                                local HitChoice = math.random(1, 3)
                                SwingAnimation:Play()
                                SwingAnimation:AdjustSpeed(1.5 + (math.random() * 0.1))
                                if
                                    JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and
                                        JeffTheKillerKnife:FindFirstChild("Swing")
                                 then
                                    local SwingSound = JeffTheKillerKnife:FindFirstChild("Swing")
                                    SwingSound.Pitch = 1 + (math.random() * 0.04)
                                    SwingSound:Play()
                                end
                                Wait(0.3)
                                if
                                    TargetModel and TargetPart and FoundHumanoid and FoundHumanoid.Health ~= 0 and
                                        (TargetPart.Position - JeffTheKillerHumanoidRootPart.Position).magnitude < 8
                                 then
                                    FoundHumanoid:TakeDamage(30)
                                    if
                                        HitChoice == 1 and JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and
                                            JeffTheKillerKnife:FindFirstChild("Hit1")
                                     then
                                        local HitSound = JeffTheKillerKnife:FindFirstChild("Hit1")
                                        HitSound.Pitch = 1 + (math.random() * 0.04)
                                        HitSound:Play()
                                    elseif
                                        HitChoice == 2 and JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and
                                            JeffTheKillerKnife:FindFirstChild("Hit2")
                                     then
                                        local HitSound = JeffTheKillerKnife:FindFirstChild("Hit2")
                                        HitSound.Pitch = 1 + (math.random() * 0.04)
                                        HitSound:Play()
                                    elseif
                                        HitChoice == 3 and JeffTheKillerScript and JeffTheKiller and JeffTheKillerKnife and
                                            JeffTheKillerKnife:FindFirstChild("Hit3")
                                     then
                                        local HitSound = JeffTheKillerKnife:FindFirstChild("Hit3")
                                        HitSound.Pitch = 1 + (math.random() * 0.04)
                                        HitSound:Play()
                                    end
                                end
                                Wait(0.1)
                                AttackDebounce = false
                            end
                        )
                    end
                end
            end
        end
    end
    return TargetMain
end
0
Please tell me this code wasn't written by you. I commend the effort, but its a mess. The answer below states your problem, the second you suggested seems like the appropriate place of application. Ziffixture 6913 — 3y
0
Thank you very much I will play with this shortly. Nah the code wasn't written by me. I have been using shared models / scripts I find in the studio database to try learning. This code was indeed a mess, like horribly, Prolly why I am having such a hard time making heads and tails of it but it's for a freddy kruiger enemy that my fiance is in love with so I am trying hard to get it to work. Thanks Sammi_Eles 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You need to format your post by highlighting the script and indenting it. Anyway, I'm 98% sure you're only checking if the target has a humanoid. What you need to do is check if the character is an actual player. To do this you can do

local player = game.Players:GetPlayerFromCharacter(TargetModel) 
if player then
    --attack
else
end
0
So this section I am guessing is the issue. I cant seem to figure out the exact place based off your comment. Im sorry. Sammi_Eles 0 — 3y
0
for _,TargetModel in pairs(game:GetService("Workspace"):GetChildren())do if JeffTheKillerScript and JeffTheKiller and JeffTheKillerHumanoid and JeffTheKillerHumanoid.Health~=0 and TargetModel.className=="Model"and TargetModel~=JeffTheKiller and TargetModel.Name~=JeffTheKiller.Name and TargetModel:FindFirstChild("Torso")and TargetModel:FindFirstChild("Head")then local TargetPart=TargetModel:Fi Sammi_Eles 0 — 3y
Ad

Answer this question