I'm not good at making scripts from scratch, not even a bit when trying to make a gun script or something else, so i take other's people models and heavily modify them to my desire.
If you have ever played in some games like Zombie Tower or Build a hideout and fight, you may know about these guns which used cFrame, welds and transparency animations ( part A set transparency to 1 and part B to make it look like it moved from part A to B ). a user called manofthelol used these features a lot in his weapon creations.
he also probably made a minigun weapon too which used mesh offsets to "rotate" the 6 barrels; i took a modified version of that model which instead of firing parts with BodyForces, it turned into a raycast weapon which when shot, it will make small part fragments which the same color as the target Part shot by it.
the weapon has a main LocalScript called "Shooter"
Tool.Enabled = true function onActivated() if not Tool.Enabled then return end Tool.Enabled = false local character = Tool.Parent; local humanoid = character.Humanoid if humanoid == nil then print("Humanoid not found") return end local ammo = script.Parent.Ammo local maxammo = script.Parent.MaxAmmo firing = true while firing == true do wait() if reloading == false and humanoid.Health >= 1 then if ammo.Value >= 1 then ammo.Value = ammo.Value - 1 local targetPos = humanoid.TargetPoint local lookAt = (targetPos - character.Head.Position).unit if script.Parent.Recoil.Value < 10 then script.Parent.Recoil.Value = script.Parent.Recoil.Value + 1 end if Spin > 50 then fire(targetPos) else wait() end end elseif reloading == false and humanoid.Health < 1 then Tool:remove() end end Tool.Enabled = true end --[[function onActivated2() if not Tool.Enabled then return end Tool.Enabled = false local character = Tool.Parent; local humanoid = character.Humanoid if humanoid == nil then print("Humanoid not found") return end local ammo = script.Parent.Ammo local maxammo = script.Parent.MaxAmmo firing = true while firing == true do wait() if reloading == false and humanoid.Health >= 1 then if ammo.Value >= 1 then ammo.Value = ammo.Value - 1 local targetPos = humanoid.TargetPoint local lookAt = (targetPos - character.Head.Position).unit if script.Parent.Recoil.Value < 10 then script.Parent.Recoil.Value = script.Parent.Recoil.Value + 1 end if Spin > 50 then Spin = 50 else wait() end end elseif reloading == false and humanoid.Health < 1 then Tool:remove() end end Tool.Enabled = true end--]]
the greened out part is supposed to be the mouse2 activation function; you can see the function is identical except it will keep changing the spin value to 50 instead of calling the fire(targetPos) function, because i want the gun to behave like the minigun you see in Valve's Team Fortress 2, if you ever played it.
the minigun's secondary fire will rev up the gun, but will not shoot and when you click Mouse1, it shoots.
https://wiki.teamfortress.com/wiki/Minigun
Players can begin spinning the barrel by holding down secondary fire, which will not fire any ammunition until primary fire is pressed. This can allow the Heavy to quickly respond to enemies when keeping the same position.
but the problem is that if you hold one of the buttons, when clicking the other one, it will stop revving or shooting, forcing the player to manually click the opposite button than you clicked first.
this is the weapon in question https://www.roblox.com/library/943150996/needs-to-be-fixed-minigun
if Spin > 50 then Spin = 50 else wait() end
my idea is to put an and if statement in the current syntax
if Spin > 50 and Button1down == true then fire(targetPos) else ??? -- what would i put here if the above syntax worked, though? end
but how can there be a value to check if button1 or 2 is being pressed or not? exactly, there isn't
then what do i have to do for the gun to not stop revving when holding mouse2 then pressing mouse1 to fire or when firing then pressing mouse2?
(i don't think this is a good topic. i tried to explain as good as i could, though.)
What you wrote is fine - all you need to do is make a bool saying whether the minigun is spinning or not. But, due to there being two buttons and two different functions for deactivating the tool, we will need two bools to compare against each other.
local LMB = false local RMB = true function OnActivated() if not RMB then -->> if the spin button is not being used LMB = true -->> do spin end -->> shoot end function OnActivated2() if not LMB then -->> if the shoot button is not being used RMB = true -->> do spin end -->> other code end function OnDeactivated() LMB = false end function OnDeactivated2() RMB = false end
Hope I could help!
~TDP
@TheDeadlyPanther
tried following your modification and it worked half-good
function onActivated() if not Tool.Enabled then return end Tool.Enabled = false local character = Tool.Parent; local humanoid = character.Humanoid if humanoid == nil then print("Humanoid not found") return end local ammo = script.Parent.Ammo local maxammo = script.Parent.MaxAmmo if not RMB then LMB = true print("Lmb is true") end firing = true while firing == true do wait() if reloading == false and humanoid.Health >= 1 then if ammo.Value >= 1 then ammo.Value = ammo.Value - 1 local targetPos = humanoid.TargetPoint local lookAt = (targetPos - character.Head.Position).unit if script.Parent.Recoil.Value < 10 then script.Parent.Recoil.Value = script.Parent.Recoil.Value + 1 end if Spin > 50 then fire(targetPos) else wait() end end elseif reloading == false and humanoid.Health < 1 then Tool:remove() end end Tool.Enabled = true end
function nofiar(mouse) if script.Parent.Handle.Fire.Playing == true then script.Parent.Handle.Fire:Stop() script.Parent.Muzzle.Flash.Enabled = false AnimTrack2:Stop() end LMB = false firing = false end function nofiar2(mouse) if script.Parent.Handle.Fire.Playing == true then script.Parent.Handle.Fire:Stop() script.Parent.Muzzle.Flash.Enabled = false AnimTrack2:Stop() end firing = false RMB = false end
(the deactivation functions turns out to be nofiar rather than Deactivate, though)
but now there are still some problems : when i completely rev up, when i press left click, it doesnt fire, but only after second-clicking LMB while still revving, then it works like i wanted; but when you press left-click first, it will normally rev up and then fire, but when pressing the right-click while holding left-click it will cancel firing when you release right click and then rev down.
also:
if firing == true or RMB == true then Spin = Spin + 2.5 else Spin = Spin - 5 end if Spin <= 0 then Spin = 0 else
maybe that could be the culprit but i don't think so..