Make npc i made attack you with magic

what i mean by it , not making the npc move to you , just making him look to the side you are at and shoot a beam on you, i have no idea how to make it can someone help me? and also i want to make that it will use random move , not just one move. so how i do that?

3 Likes

Can you share some Code of what you have so far?

2 Likes

all i have is npc without code, i have no idea how to even start doing that thing, i tried to find something in youtube but all the tutorials are “npc moving to you and making the hand as a damage part”

2 Likes

I found this by googling for it, it may work for you.

What I can suggest here is raycasting, read more about it here:

You can use tables and math.random() for this.

local Moves = {
 function(TargetPlayer)
  -- do raycasting and stuff
 end,
 function(TargetPlayer)
  -- do raycasting and stuff for the other move
 end
}

function GetRandomMove()
 return Moves[math.random(#Moves)]
end

If you have any questions about this GetRandomMove function and what exactly it does, don’t hesitate to reply!

4 Likes

ok, i found info about the first one, but my npc kind of broken,How i fix that?

https://gyazo.com/9ee57f663b44503d163a1b4f55e1468c

2 Likes

Could we see the code if possible so we can determine what is wrong?

Here’s the code:

local char = script.Parent
local npcToFind = workspace.Gaster

spawn(function()
	while true do
		local TO = char.HumanoidRootPart
		local dist = (TO.Position - npcToFind.HumanoidRootPart.Position).magnitude
		if dist <= 50 then
			npcToFind.HumanoidRootPart.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0))
		end
		wait()
	end
end)

but i think the problem not in the code, the problem on the npc

Did you modify the NPC itself?

1 Like

i took a human from the “rig builder” and modified it, and resized it a bit

That’s most likely the problem, try it on a humanoid which you did not resize and just added a shirt/pants/accessories.

ok it fixed it, now i’ll try to do the moves

2 Likes

i did something like this:

   local char = script.Parent
    local npcToFind = workspace.Gaster

local char = script.Parent
local npcToFind = workspace.Gaster

local Moves = {
 function(TargetPlayer)
 local ray = Ray.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0)*400)
 local part, position = workspace:FindPartOnRay(ray, char, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (npcToFind.HumanoidRootPart.Position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, position) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
end,
function(TargetPlayer)
 local ray = Ray.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0)*400)
 local part, position = workspace:FindPartOnRay(ray, char, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright blue")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (npcToFind.HumanoidRootPart.CFrame).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, position) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
end
}
spawn(function()
	while true do
		local TO = char.HumanoidRootPart
		local dist = (TO.Position - npcToFind.HumanoidRootPart.Position).magnitude
		if dist <= 50 then
			npcToFind.HumanoidRootPart.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0))
			local function GetRandomMove()
				return Moves[math.random(#Moves)]
			end
		end
		wait()
	end
end)

it doesn’t works, but is this close to what i suppose to do?

1 Like

Heres your problem: You put the GetRandomMove function inside of the while true, create the function only once. Next thing is that you are never calling the function, hence why nothing is happening. Switch everything starting from spawn(function() out with this:

local function GetRandomMove()
 return Moves[math.random(#Moves)]
end

spawn(function()
	while true do
		local TO = char.HumanoidRootPart
		local dist = (TO.Position - npcToFind.HumanoidRootPart.Position).magnitude
		if dist <= 50 then
			npcToFind.HumanoidRootPart.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0))
			local Move = GetRandomMove()
                        Move(game.Players:GetPlayerFromCharacter(char))
		end
		wait()
	end
end)

This should do it’s job, might also needs a bit of modifying on your side.

1 Like

oh it works, but it don’t shoot the right way
https://gyazo.com/9007bb2e696e70a7426ce2af9fc880ca
and how i make it don’t damage itself

1 Like

I saw multiple mistakes you made with the raycasting and distance calculation. Here are the fixed parts of your code and lets hope that works:

local ray = Ray.new(npcToFind.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.p - npcToFind.HumanoidRootPart.CFrame.p).unit * 300)
local distance = (npcToFind.HumanoidRootPart.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(npcToFind.HumanoidRootPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

i tried to make the move to not spam, but it also makes the npc to not move always
https://gyazo.com/1e1fb2207fdf85301fefbca94c6f8f1a

and also the beam doesn’t damage to me and doesn’t shoot where it suppose to, how i make the beam direction to HumanoidRootPart, and how i make the hand that flying move with the npc?

1 Like

Can you send your new version of your code?

there:

local char = script.Parent
local npcToFind = workspace.Gaster

local Moves = {
 function(TargetPlayer)
	wait(2)
 local ray = Ray.new(char.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.p - npcToFind.HumanoidRootPart.CFrame.p).unit * 300)
 local part, position = workspace:FindPartOnRay(ray, char, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (char.HumanoidRootPart.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(npcToFind.HumanoidRootPart["Gaster Hand"]["Hole Part"].CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(1)
			end
		end
end,
function(TargetPlayer)
	wait(5)
local ray = Ray.new(npcToFind.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.p - npcToFind.HumanoidRootPart.CFrame.p).unit * 300)
 local part, position = workspace:FindPartOnRay(ray, char, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright blue")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (npcToFind.Head.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(npcToFind.Head.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
				
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
end
}
local function GetRandomMove()
 return Moves[math.random(#Moves)]
end

spawn(function()
	while true do
		local TO = char.HumanoidRootPart
		local dist = (TO.Position - npcToFind.HumanoidRootPart.Position).magnitude
		if dist <= 50 then
			npcToFind.HumanoidRootPart.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0))
			local Move = GetRandomMove()
                        Move(game.Players:GetPlayerFromCharacter(char))				
		end
		wait()
	end
end)
1 Like

This is something you have to figure out yourself, see it as a challenge.


Try this version, I fixed some common scripting mistakes you did including waiting at the wrong position and adding a debounce for using attacks:

local char = script.Parent
local npcToFind = workspace.Gaster

local Moves = {
    function(TargetPlayer)
        local ray = Ray.new(char.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.p - npcToFind.HumanoidRootPart.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, char, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (char.HumanoidRootPart.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(npcToFind.HumanoidRootPart["Gaster Hand"]["Hole Part"].CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.1)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(1)
			end
        end
        return 2 -- The time in seconds you want to wait before the next attack
    end,
    function(TargetPlayer)
        local ray = Ray.new(npcToFind.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.p - npcToFind.HumanoidRootPart.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, char, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright blue")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (npcToFind.Head.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(npcToFind.Head.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
				
		game:GetService("Debris"):AddItem(beam, 0.1)
        
        if part then
            print("part found")
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(30)
            end
        else
            warn("part not found")
        end
        return 5 -- The time in seconds you want to wait before the next attack
    end
}

local function GetRandomMove()
    return Moves[math.random(#Moves)]
end

local lastAttack = 0

spawn(function()
	while true do
		local TO = char.HumanoidRootPart
		local dist = (TO.Position - npcToFind.HumanoidRootPart.Position).magnitude
		if dist <= 50 then
			npcToFind.HumanoidRootPart.CFrame = CFrame.new(npcToFind.HumanoidRootPart.Position, char.HumanoidRootPart.Position*Vector3.new(1,0,1) + char.HumanoidRootPart.Position*Vector3.new(0, 1, 0))
            if lastAttack <= tick() then
                local Move = GetRandomMove()
                local returnedTime = Move(game.Players:GetPlayerFromCharacter(char))
                lastAttack = tick() + returnedTime
            end
		end
		wait()
	end
end)
1 Like

it moves around , but it’s still don’t shoot to the humanoidrootpart and it doesn’t take damage

1 Like