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

I'm trying to figure out a few things about a gun turret. Can someone help me?

Asked by
marfit 104
6 years ago
Edited 6 years ago

I'm making a gun turret to practice my raycasting abilities, and I am following a tutorial, but I have a problem. A few, actually. First, and biggest, in the findTarget function the script generates a ray, and multiplies it by 500 with a .Unit . What does that do? Second, I want to use that ray to make sure that its not firing at a wall the humanoid is hiding behind, but instead, the humanoid itself. Third, how would I add inaccuracy to my gun? Fourth, the FireBullet() function works, but how do I make the sound sound like its overlapping, it doesn't sound right, its sounds glitchy. Heres the code:

01gun = script.Parent
02 
03function FindTarget()
04    local dist = 100
05    local target = nil
06    for i,v in pairs(workspace:GetChildren()) do
07        local torso = v:FindFirstChild("Torso")
08        local humanoid = v:FindFirstChild("Humanoid")
09        if torso and humanoid and humanoid.Health > 0 then
10            if (gun.Position - torso.Position).magnitude < dist then
11                local ray = Ray.new(gun.Position, (gun.Position- torso.Position).Unit*500)
12                local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{torso.Parent})
13                if hit == nil then
14                    dist = (gun.Position - torso.Position).magnitude
15                    target = torso
View all 55 lines...

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I believe your problem is..

.~~~~~~~~~~~~~~~~~ for i,v in pairs(workspace:GetChildren()) do

1I believe its this because not everything in the workspace may have a child 'Humanoid' which will trigger an error that will discontinue the code.
2 
3You can use a pcall function or make it simple... But it will only work on Players
1for i, v in pairs(game.Players:GetPlayers()) do
2    wait()
3 
4    local Character = v.Character
5end
1Another reason it may be breaking when player character code runs the player isn't fully loaded...
1for i,v in pairs(workspace:GetChildren()) do
2    wait()
3    --Code
4end

~~~~~~~~~~~~~~~~~

0
The gun itself works, the bullets fire at me and will hurt me, but I don't know how to make it stop firing when it stops seeing me (IE, go behind a wall) marfit 104 — 6y
Ad

Answer this question