Hello, so basicly i have a gun script that u can hold down to shoot or simply press once to shoot once, it kinda works except it only runs the ray if u hold down and have shot 1 bullet before for some reason? its the same for the bullet that is supposed to fly out of the gun. the rest of the function gets executed perfectly. I cant figure it out and have tried for many hours now so would be very nice to get some help :)
Shooting = false
GunData = require(game.ReplicatedStorage.GunData)
TestGunData = GunData.TestGun
GunDMG = TestGunData[1]
GunRLT = TestGunData[2]
GunRPM = TestGunData[3]
GunMA = TestGunData[4]
Auto = TestGunData[5]
BType = TestGunData[6]
CurrentBullets = game.ReplicatedStorage[player.Name].CurrentAmmo
BulletModel = game.ReplicatedStorage.Bullets[BType]
Muzzle = weapon.Muzzle
MuzzleFlash = weapon.MuzzleFlash
RecoilGoal = {}
RecoilTweenInfo = TweenInfo.new(0.05, Enum.EasingStyle.Linear)
function ShootOnce()
print("ShootOnce called")
local BulletRay = Ray.new(weapon.Muzzle.Position, game.Workspace.CurrentCamera.CFrame.LookVector * TestGunData[9])
if BulletRay then
print("There is a bullet ray", weapon.Muzzle.Position)
end
local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(BulletRay, {workspace.PlayerWeapons[player.Name], player.Character, workspace.DebrisBulletHoles})
if hitPart then
print("The ray touched something")
local NewBulletHole = Instance.new("Part", game.Workspace.DebrisBulletHoles)
NewBulletHole.Name = "BulletHole"
NewBulletHole.Anchored = true
NewBulletHole.CanCollide = false
NewBulletHole.Position = hitPosition
NewBulletHole.Size = Vector3.new(1, 1, 1)
Debris:AddItem(NewBulletHole, 5)
end
CurrentBullets.Value = CurrentBullets.Value - 1
Muzzle.MuzzleFlashLight.Enabled = true
--MuzzleFlash.CFrame = Muzzle.CFrame * CFrame.new(1, 0, 0)
--MuzzleFlash.Orientation = Vector3.new(math.random(0, 90), 0, 0)--MuzzleFlash.CFrame * CFrame.Angles(0, 0, math.rad(math.random(0, 90)))
MuzzleFlash.FlashImage.Transparency = 0
local clonedBullet = BulletModel:Clone()
clonedBullet.Parent = workspace.DebrisBullets
clonedBullet.CFrame = weapon.BulletPart.CFrame
clonedBullet.BodyThrust.Force = Vector3.new(0, 0, -400)
if Aiming then
joint.C0 = TestGunData[8] * CFrame.new(1, 0, 0)
else
joint.C0 = TestGunData[7] * CFrame.new(1, 0, 0)
end
wait(0.01)
if Aiming then
RecoilGoal.C0 = TestGunData[8]
else
RecoilGoal.C0 = TestGunData[7]
end
local RecoilTween = TS:Create(joint, RecoilTweenInfo, RecoilGoal)
RecoilTween:Play()
clonedBullet.BodyThrust.Force = Vector3.new(0, 0, 0)
Muzzle.MuzzleFlashLight.Enabled = false
MuzzleFlash.FlashImage.Transparency = 1
Debris:AddItem(clonedBullet, 2)
end
function StartShooting() -- Shoot function to start fire bullets
if CurrentBullets.Value > 0 then
if Auto == false and not Shooting then
Shooting = true
ShootOnce()
wait(60/GunRPM)
Shooting = false
elseif Auto == true then
while Shooting == true and CurrentBullets.Value > 0 do
ShootOnce()
wait(60/GunRPM)
end
end
end
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if Reloading == false then
Shooting = true
StartShooting()
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
Shooting = false
end
end)
if needed, here is the WHOLE script
UIS = game:GetService("UserInputService")
TS = game:GetService("TweenService")
Debris = game:GetService("Debris")
camera = game.Workspace.CurrentCamera
humanoid = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("Humanoid")
player = game.Players.LocalPlayer
Aiming = false
--Set the mouse cursor
UIS.MouseIconEnabled = false
--[[mouse = player:GetMouse()
mouse.Icon = ""]]
viewModel = game.ReplicatedStorage:WaitForChild("viewModel"):Clone()
function onDied()
viewModel.Parent = nil
end
function onUpdate(dt)
viewModel.Head.CFrame = camera.CFrame
end
humanoid.Died:Connect(onDied)
game:GetService("RunService").RenderStepped:Connect(onUpdate)
--Attach weapon to the viewModel
local repWeapon = game.ReplicatedStorage:WaitForChild("TestGun")
local weapon = repWeapon:Clone()
weapon.Parent = viewModel
viewModel.Parent = workspace.PlayerWeapons:WaitForChild(player.Name)
local joint = Instance.new("Motor6D")
joint.C0 = CFrame.new(1, -1.5, -2) * CFrame.Angles(0, math.rad(270), 0)
joint.Part0 = viewModel.Head
joint.Part1 = weapon.Handle
joint.Parent = viewModel.Head
--Tween For Aiming
AimTweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Linear)
Goal = {}
--Start Aiming
function StartAiming()
Goal.C0 = TestGunData[8]
local StartAimingTween = TS:Create(joint, AimTweenInfo, Goal)
StartAimingTween:Play()
Aiming = true
--joint.C0 = CFrame.new(0, -1.25, 0) * CFrame.Angles(0, math.rad(270), 0)
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
StartAiming()
end
end)
--Stop Aiming
function StopAiming()
Goal.C0 = TestGunData[7]
local StopAimingTween = TS:Create(joint, AimTweenInfo, Goal)
StopAimingTween:Play()
Aiming = false
--joint.C0 = CFrame.new(1, -1.5, -2) * CFrame.Angles(0, math.rad(270), 0)
end
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
StopAiming()
end
end)
--Toggle Aim
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then
if Aiming then
StopAiming()
else
StartAiming()
end
end
end)
-- Shoot
Shooting = false
GunData = require(game.ReplicatedStorage.GunData)
TestGunData = GunData.TestGun
GunDMG = TestGunData[1]
GunRLT = TestGunData[2]
GunRPM = TestGunData[3]
GunMA = TestGunData[4]
Auto = TestGunData[5]
BType = TestGunData[6]
CurrentBullets = game.ReplicatedStorage[player.Name].CurrentAmmo
BulletModel = game.ReplicatedStorage.Bullets[BType]
Muzzle = weapon.Muzzle
MuzzleFlash = weapon.MuzzleFlash
RecoilGoal = {}
RecoilTweenInfo = TweenInfo.new(0.05, Enum.EasingStyle.Linear)
function ShootOnce()
print("ShootOnce called")
local BulletRay = Ray.new(weapon.Muzzle.Position, game.Workspace.CurrentCamera.CFrame.LookVector * TestGunData[9])
if BulletRay then
print("There is a bullet ray", weapon.Muzzle.Position)
end
local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(BulletRay, {workspace.PlayerWeapons[player.Name], player.Character, workspace.DebrisBulletHoles})
if hitPart then
print("The ray touched something")
local NewBulletHole = Instance.new("Part", game.Workspace.DebrisBulletHoles)
NewBulletHole.Name = "BulletHole"
NewBulletHole.Anchored = true
NewBulletHole.CanCollide = false
NewBulletHole.Position = hitPosition
NewBulletHole.Size = Vector3.new(1, 1, 1)
Debris:AddItem(NewBulletHole, 5)
end
CurrentBullets.Value = CurrentBullets.Value - 1
Muzzle.MuzzleFlashLight.Enabled = true
--MuzzleFlash.CFrame = Muzzle.CFrame * CFrame.new(1, 0, 0)
--MuzzleFlash.Orientation = Vector3.new(math.random(0, 90), 0, 0)--MuzzleFlash.CFrame * CFrame.Angles(0, 0, math.rad(math.random(0, 90)))
MuzzleFlash.FlashImage.Transparency = 0
local clonedBullet = BulletModel:Clone()
clonedBullet.Parent = workspace.DebrisBullets
clonedBullet.CFrame = weapon.BulletPart.CFrame
clonedBullet.BodyThrust.Force = Vector3.new(0, 0, -400)
if Aiming then
joint.C0 = TestGunData[8] * CFrame.new(1, 0, 0)
else
joint.C0 = TestGunData[7] * CFrame.new(1, 0, 0)
end
wait(0.01)
if Aiming then
RecoilGoal.C0 = TestGunData[8]
else
RecoilGoal.C0 = TestGunData[7]
end
local RecoilTween = TS:Create(joint, RecoilTweenInfo, RecoilGoal)
RecoilTween:Play()
clonedBullet.BodyThrust.Force = Vector3.new(0, 0, 0)
Muzzle.MuzzleFlashLight.Enabled = false
MuzzleFlash.FlashImage.Transparency = 1
Debris:AddItem(clonedBullet, 2)
end
function StartShooting() -- Shoot function to start fire bullets
if CurrentBullets.Value > 0 then
if Auto == false and not Shooting then
Shooting = true
ShootOnce()
wait(60/GunRPM)
Shooting = false
elseif Auto == true then
while Shooting == true and CurrentBullets.Value > 0 do
ShootOnce()
wait(60/GunRPM)
end
end
end
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if Reloading == false then
Shooting = true
StartShooting()
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
Shooting = false
end
end)
-- Reload
Reloading = false
function Reload()
Reloading = true
wait(GunRLT)
CurrentBullets.Value = GunMA
Reloading = false
end
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
if CurrentBullets.Value < GunMA and Reloading == false and Shooting == false then
Reload()
end
end
end)
