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

Why Is This Sentry Gun Script Malfunctioning?

Asked by 9 years ago

What this is supposed to do is whenever the player is near, it fires bullets at it. Here is the script:

local part = script.Parent
local player = game.Players.LocalPlayer
local damage = 10
local mouse = player:GetMouse()
local range = 100

    function Range ()
    local mag = (part.CFrame.p - player.Character.Torso.CFrame.p).magnitude
    part.Parent.Rotation = player.Character.Torso.Position  
    if mag >= range then
        local bullet = Instance.new("Part", game.Workspace)
        bullet.Name = "Bullet"
        bullet.Shape = "Ball"
        bullet.FormFactor = "Custom"
        bullet.Size = Vector3.new(0.2,0.2,0.2)
        bullet.TopSurface = "Smooth"
        bullet.BottomSurface = "Smooth"
        bullet.BrickColor = BrickColor.new("Medium stone grey")
        bullet.CanCollide = false
        bullet.CFrame = part.CFrame
        bullet.CFrame = CFrame.new(bullet.Position, player.Character.Torso.p)
        local v = Instance.new("BodyVelocity", bullet)
        v.Velocity = bullet.CFrame.lookVector * 90
        v.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    end
    end 
    Range()
0
Line 24 can be shorter: v.MaxForce = Vector3.new(1,1,1)*math.huge EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Thank you LordDragon, I fixed it on my own though, here is my new problem: The problem is at Line 7, where the while loop is introduced.

local part = script.Parent
local damage = 10
local range = 10

        game.Players.PlayerAdded:connect(function(char)
        char.CharacterAdded:connect(function()
        while (char.Character.Torso.CFrame.p - part.CFrame.p).magnitude <= range do
        local bullet = Instance.new("Part", game.Workspace)
        bullet.Name = "Bullet"
        bullet.Shape = "Ball"
        bullet.FormFactor = "Custom"
        bullet.Size = Vector3.new(0.2,0.2,0.2)
        bullet.Transparency = 0
        bullet.TopSurface = "Smooth"
        bullet.BottomSurface = "Smooth"
        bullet.BrickColor = BrickColor.new("Bright yellow")
        bullet.CanCollide = false
        bullet.CFrame = part.CFrame
        bullet.CFrame = CFrame.new(bullet.Position, char.Character.Torso.Position)
        local v = Instance.new("BodyVelocity", bullet)
        v.velocity = bullet.CFrame.lookVector * 90
        v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        part.Fire:Play()
        local hum = char.Character:FindFirstChild("Humanoid")
        hum:TakeDamage(damage)
        game.Debris:AddItem(bullet, 2)
        wait(0.5)
            end
            end)
            end)
0
bump Noobegai -4 — 9y
0
There's an "edit" feature to edit your question Redbullusa 1580 — 9y
Ad

Answer this question