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

How can my gunscript work perfectly for one player but not for the other?[semi-solved, closed]

Asked by
AZDev 590 Moderation Voter
8 years ago

I have no better way to explain this other then a visual example of what it happening.

--player variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

--gun variables
local gun = script.Parent 
local ammo = 15
local magammo = ammo
local reloading = false
local auto = false -- set to false if you do not want the gun to be automatic
local firing = false
local CanChangeFireModes = false 
--^^If set to true the player can press v to change firemodes from auto to semi and semi to auto

local debounce = false

if not game.workspace:FindFirstChild("Bullet") then
    local bullet_storage = Instance.new("Folder", workspace)
    bullet_storage.Name = "Bullet"
else
    --nothing
end

function getBullet()
    print(ammo)
    if ammo > 0 then
        if debounce == false then
            debounce = true
            ammo = ammo - 1
            gun.Handle.Fire:Play()
            local mh = mouse.Hit
            local B = Instance.new("Part", workspace.Bullet)
            game:GetService("Debris"):AddItem(B, 2)
            B.CFrame = gun.Main.CFrame + (player.Character.Torso.CFrame.lookVector)
            B.CFrame = CFrame.new(B.Position, mh.p)
            B.FormFactor = "Custom"
            B.Size = Vector3.new(0.2, 0.2, 0.2)
            B.BrickColor = BrickColor.Black()
            B.CanCollide = false
            local velo = Instance.new("BodyVelocity", B)
            velo.velocity = B.CFrame.lookVector * 120
            velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            B.Touched:connect(function(hit)
                wait(0.001)
                B:Destroy()
                local playerHit = game.Players:GetPlayerFromCharacter(hit.Parent)
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if playerHit and humanoid and player.TeamColor ~= playerHit.TeamColor then
                    humanoid:TakeDamage(25, 30)
                end
            end)
        wait(0)
        debounce = false
        end
    end
end

function autofire()
    if ammo > 0 then
        while firing == true do
            if firing == true and ammo <= 0 then
                firing = false
            end
            print(ammo)
            wait(0)
            gun.Handle.Fire:Play()
            local mh = mouse.Hit
            local B = Instance.new("Part", workspace.Bullet)
            game:GetService("Debris"):AddItem(B, 2)
            B.CFrame = gun.Main.CFrame + (player.Character.Torso.CFrame.lookVector)
            B.CFrame = CFrame.new(B.Position, mh.p)
            B.FormFactor = "Custom"
            B.Size = Vector3.new(0.2, 0.2, 0.2)
            B.BrickColor = BrickColor.Black()
            B.CanCollide = false
            ammo = ammo - 1
            local velo = Instance.new("BodyVelocity", B)
            velo.velocity = B.CFrame.lookVector * 120
            velo.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            B.Touched:connect(function(hit)
                wait(0.001)
                B:Destroy()
                local playerHit = game.Players:GetPlayerFromCharacter(hit.Parent)
                local humanoid = hit.Parent:FindFirstChild("Humanoid")
                if playerHit and humanoid and player.TeamColor ~= playerHit.TeamColor then
                    humanoid:TakeDamage(25, 30)
                end
            end)
        wait(0.005)
        end
    end
end

mouse.Button1Down:connect(function()
    if auto == true then
        firing = true
        autofire()
    else
        getBullet()
    end
    if ammo <= 0 then
        print('reloading')
        wait(1)
        ammo = magammo
    end
end)

mouse.Button1Up:connect(function()
    if firing == true then
        firing = false
    end
end)

local uis = game:GetService("UserInputService") -- allows firemode change
uis.InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.V and CanChangeFireModes == true then
        if auto == true then
            auto = false
        else
            auto = true
        end
    end
end)

This is the script that is messing up.

What is happening? When one player shoots the bullets will fire correctly. If there is another player in the game and he shoots the gun the bullets act as if they hit something and fly back at the player who shot it.

For a visual example you can go here: http://www.roblox.com/games/347680497/Desert-Airsoft-Pre-Alpha-update

0
Reverting game back by 15 updates seems to have fixed the issue. Something I added must have messed it up. Might have been Epix Incorporated Server Suite. AZDev 590 — 8y

Answer this question