Well first of all i tried to put reload in to a semi-automatic gun. It works!
But here are the issues…
First when the gun finishes the last bullet the reload isn’t done instantly, instead you have to click again for the gun to reload, and when it does reload well everything goes according to plan except that once the reloading is complete the gun actually shoots even if you didn’t click the mouse. It does make sense due to how i made the script(Local script) But i just couldn’t fix this, so in conclusion the gun does reload but i want the gun to reload instantly once the gun runs out of bullets, and second how the hell do i fix the gun shooting itself once done reloading
Here is the local script:
local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastP = RaycastParams.new()
raycastP.FilterDescendantsInstances = {player.Character}
local ammo = 6
local max_ammo = 6
local reloading = false
local function reload()
tool.Handle.Reload:Play()
reloading = true
task.wait(1)
ammo = max_ammo
reloading = false
task.wait(1)
end
tool.Activated:Connect(function()
if shooting == false and reloading == false then
if ammo == 0 then
reload()
end
ammo -= 1
tool.Handle.Fire:Play()
local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastP)
if raycast then
local instance = raycast.Instance
local instanceParent = instance.Parent
local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")
if findHumanoid then
game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
end
end
shooting = true
task.wait(2)
shooting = false
end
end)
you check the ammo amount before you fire the bullet so it won’t happen after you have fired, in that case you have to put this line at the end of the fire function. Next up you don’t actually stop the function after reloading so it will make an odd thing where you automatically fire after the reload is done, you can add return after reload to stop the function
Maybe try this code, this code is an easy fix to make the gun reload with 1 ammo or more left.
mouse.Button1Down:Connect(function()
if ammo == 0 then
reload()
end
ammo -= 1
tool.Handle.Fire:Play()
local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastP)
if raycast then
local instance = raycast.Instance
local instanceParent = instance.Parent
local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")
if findHumanoid then
game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
end
end
shooting = true
task.wait(2)
shooting = false
end)
local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
raycastfilter.FilterDescendantsInstances = {player.Character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local reloading = false
local function reload()
tool.Handle.Reload:Play()
reloading = true
task.wait(2)
ammo = max_ammo
reloading = false
end
tool.Activated:Connect(function()
if not shooting and not reloading then
if ammo == 0 then
reload()
return
end
ammo -= 1
tool.Handle.Fire:Play()
local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
if raycast then
local instance = raycast.Instance
local instanceParent = instance.Parent
local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")
if findHumanoid then
game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
end
end
shooting = true
task.wait(1)
shooting = false
end
end)