- What do you want to achieve? Keep it simple and clear!
I’ m having some troubles with my gun tool. As long as I hold down the left mouse button, the gun continues to fire after reloading. I expect the gun to stop firing after reloading, making player to hold mouse again to fire.
- What is the issue? Include screenshots / videos if possible!
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to break the while loop, but it doesn’t work.
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local camera = workspace.CurrentCamera
local tool = script.Parent
local fireEvent = tool:WaitForChild("Fire")
local reloadEvent = tool:WaitForChild("Reload")
local module = require(tool.Config)
local equipped = false
local fireRate = module.fireRate
local ammoInMag = module.ammoInMag
local ammo = module.ammo
local LeftButtonDown = false
local fireTick = tick()
function GetMousePosition(X, Y)
local cameraRay= camera:ViewportPointToRay(X, Y)
local newRay = Ray.new(cameraRay.Origin, cameraRay.Direction*1000000)
local target, position = workspace:FindPartOnRay(newRay, player.Character)
return position
end
tool.Equipped:Connect(function()
equipped = true
CAS:BindAction("Reload", function(name, state, obj)
if state == Enum.UserInputState.Begin then
reloadEvent:FireServer()
end
end, false, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
equipped = false
LeftButtonDown = false
end)
UIS.InputBegan:connect(function(input, gui)
if not equipped or gui then return end --prevent player from firing by interacting with GUIs
if input.UserInputType == Enum.UserInputType.MouseButton1 then
LeftButtonDown = true
if tick()-fireTick < fireRate then return end
fireTick = tick()
while LeftButtonDown == true and ammoInMag > 0 do
local location = UIS:GetMouseLocation()
fireEvent:FireServer(GetMousePosition(location.x,location.y))
task.wait(fireRate)
end
end
end)
UIS.InputEnded:Connect(function(input, gui)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
LeftButtonDown = false
end
end)
glad if you help!