I am trying to make an automatic gun that an NPC shoots, it seems to work but the raycast is going in the wrong direction.
Here’s the script:
local shootPart = script.Parent
while wait(math.random(4,8)) do
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local rayCastResults = workspace:Raycast(shootPart.Position,shootPart.Position + Vector3.new(0,0,100))
local newPart = Instance.new("Part",workspace)
newPart.Size = shootPart.Size
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(1)
local goals = {["Position"] = (rayCastResults.Position)}
local track = TS:Create(newPart,TI,goals)
track:Play()
newPart.Anchored = true
newPart.Material = Enum.Material.Neon
newPart.BrickColor = BrickColor.Yellow()
game:GetService("Debris"):AddItem(newPart,5)
newPart.Touched:Connect(function(hit)
local humanoid = hit.Parent:WaitForChild("Humanoid")
if humanoid then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
humanoid:TakeDamage(10)
end
end
end)
end
The second parameter for raycast is the direction. You can use the LookVector instead of the position + a vector3 value. Make sure the LookVector for the shootPart is the right way though.
Fixed code:
--//Services
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")
--//Variables
local shootPart = script.Parent
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
--//Controls
local tweenInfo = TweenInfo.new(1)
--//Loops
while task.wait(Random.new():NextInteger(4, 8)) do
local rayCastResults = workspace:Raycast(shootPart.Position, shootPart.CFrame.LookVector * 100)
local newPart = Instance.new("Part")
newPart.Size = shootPart.Size
newPart.Anchored = true
newPart.Material = Enum.Material.Neon
newPart.BrickColor = BrickColor.Yellow()
newPart.Parent = workspace
local track = TS:Create(newPart, tweenInfo, {
Position = rayCastResults.Position
})
track:Play()
task.delay(5, newPart.Destroy, newPart)
newPart.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local character = player and player.Character
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end)
end
--//Services
local Players = game:GetService("Players")
--//Variables
local shootPart = script.Parent
--//Loops
while task.wait(Random.new():NextInteger(4, 8)) do
local rayCastResults = workspace:Raycast(shootPart.Position, shootPart.CFrame.LookVector * 100)
local newPart = Instance.new("Part")
newPart.Size = shootPart.Size
newPart.Anchored = false
newPart.Material = Enum.Material.Neon
newPart.BrickColor = BrickColor.Yellow()
newPart.Parent = workspace
newPart:ApplyImpulse(shootPart.CFrame.LookVector * 1000)
task.delay(5, newPart.Destroy, newPart)
newPart.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local character = player and player.Character
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end)
end
Could be because I made the bullet speed too fast.
Try this:
--//Services
local Players = game:GetService("Players")
--//Variables
local shootPart = script.Parent
--//Loops
while task.wait(Random.new():NextInteger(4, 8)) do
local newPart = Instance.new("Part")
newPart.Size = shootPart.Size
newPart.Position = shootPart.Position + shootPart.CFrame.LookVector * 5
newPart.Anchored = false
newPart.Material = Enum.Material.Neon
newPart.BrickColor = BrickColor.Yellow()
newPart.Parent = workspace
newPart:ApplyImpulse(shootPart.CFrame.LookVector * 1000)
task.delay(5, newPart.Destroy, newPart)
newPart.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
local character = player and player.Character
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
end
end)
end
Are you sure you made the shootpart appear the right way? Also, if you could send me the model in a .rbxm so I can test it by myself and then send it back, it would be appreciated.
This one uses TweenService to visualize the bullet and a raycast to damage the player.
The other one uses ApplyImpulse to visualize the bullet and .Touched to damage the player.