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

My gun script [Doesnt work in play mode] Why?

Asked by 10 years ago

This code is in a script

local tool = script.Parent
local user
local ammo = script.Parent.ammo
enabled = false
gun = script.Parent
damage = 10

--when the tool is equipped
tool.Equipped:connect(function(mouse)
    --store the character of the person using the tool
    user = tool.Parent 

    --when the left mouse button is clicked
    mouse.Button1Down:connect(function() 
        if ammo.Value > 0 and enabled == false then
            ammo.Value = ammo.Value - 1
            enabled = true
            Bullet = Instance.new("Part", workspace)
            game.Debris:AddItem(Bullet, 2)
            Bullet.Shape = "Ball"
            bm = Instance.new("SpecialMesh", Bullet)
            bm.MeshType = "Sphere"
            bm.Scale = Vector3.new(0.2,0.2,0.2)
            Bullet.Size = Vector3.new(0.1, 0.1 ,0.1)
            Bullet.TopSurface = "Smooth"
            Bullet.BottomSurface = "Smooth"
            Bullet.BrickColor = BrickColor.new("Black")
            Bullet.CanCollide = false
            Bullet.CFrame = gun.Fire.CFrame
            Bullet.CFrame = CFrame.new(Bullet.Position ,mouse.Hit.p)
            v = Instance.new("BodyVelocity", Bullet)
            v.velocity = Bullet.CFrame.lookVector*90
            v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            script.Parent.Handle.PewPew:Play()
            enabled = false 
            Bullet.Touched:connect(takeDamage)
    end
    end)
end)


    function takeDamage(part)
        if part.Parent:FindFirstChild("Humanoid") then
            if tookDamage == false then
                tookDamage = true
                part.Parent.Humanoid:TakeDamage(damage)
                Bullet.Parent = nil
                tookDamage = false
            end 
        end
        Bullet.Parent = nil
    end

And then this is in a local script

local player = game.Players.LocalPlayer

gun = script.Parent

ammo = script.Parent.ammo

mouse = player:GetMouse()

enabled = false

damage = 10

tookDamage = false

function shoot()
    if ammo.Value > 0 and enabled == false then
        ammo.Value = ammo.Value - 1
        enabled = true
        Bullet = Instance.new("Part", workspace)
        game.Debris:AddItem(Bullet, 2)
        Bullet.Shape = "Ball"
        bm = Instance.new("SpecialMesh", Bullet)
        bm.MeshType = "Sphere"
        bm.Scale = Vector3.new(0.2,0.2,0.2)
        Bullet.Size = Vector3.new(0.1, 0.1 ,0.1)
        Bullet.TopSurface = "Smooth"
        Bullet.BottomSurface = "Smooth"
        Bullet.BrickColor = BrickColor.new("Black")
        Bullet.CanCollide = false
        Bullet.CFrame = gun.Fire.CFrame
        Bullet.CFrame = CFrame.new(Bullet.Position ,mouse.Hit.p)
        v = Instance.new("BodyVelocity", Bullet)
        v.velocity = Bullet.CFrame.lookVector*90
        v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        script.Parent.Handle.PewPew:Play()
        enabled = false 
        Bullet.Touched:connect(takeDamage)
    end
end

script.Parent.Equipped:connect(function(mouse)
    if player.PlayerGui:FindFirstChild("GunHud") then
        print("HASGUI")
    else
        mouse.Icon = "http://www.roblox.com/asset/?id=152695139"
        local hud = script.Parent.GunHud:Clone()
        hud.Parent = player.PlayerGui
    end
end)

script.Parent.Unequipped:connect(function()
    if player.PlayerGui:FindFirstChild("GunHud") then
        player.PlayerGui.GunHud:Remove()
    end
end)

local function onKeyDown(key)
    print("Key:", key, " Code:", string.byte(key))

if key == "r" then
    if script.Parent.ammo.Value == 0 then
        local anim = player.Character.Humanoid:LoadAnimation(script.Parent.Reload)
        anim:Play()
        script.Parent.Handle.reloadSound:Play()
        wait(0.5)
        script.Parent.Handle.reloadSound:Play()
        wait(0.5)
        script.Parent.Handle.reloadSound:Play()
        wait(2)
        script.Parent.ammo.Value = 1
    end
end

end

function takeDamage(part)
    if part.Parent:FindFirstChild("Humanoid") then
        if tookDamage == false then
            tookDamage = true
            part.Parent.Humanoid.Health = part.Parent.Humanoid.Health - damage
            Bullet.Parent = nil
            tookDamage = false
        end 
    end
end

script.Parent.ammo.Changed:connect(function(Val)
    player.PlayerGui.GunHud.Hud.HudText.Text = "Ammo " ..script.Parent.ammo.Value.. "/1"
end)

gun.Activated:connect(shoot)
mouse.KeyDown:connect(onKeyDown)

They are both in the gun, the gun works in play solo in edit, but doesnt work in multiplayer play mode.

Answer this question