I’m working on a Raycast gun and i need help to work things out with its inaccuracy.
My idea is that the longer the player shoots from it, the bigger inaccuracy becomes until it reaches some limit. The problem I have is i can’t make up any good solutions to detecting when the player stopps holding the shooting button down, so I could reset the guns inaccuracy.
here’s the code that I use
FireEvent.OnServerEvent:Connect(function(player,mousePos)
if equipped == false then return end
if game.Players:GetPlayerFromCharacter(tool.Parent) ~= player then return end
local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
if humanoid.Health <= 0 then return end
if not ((tick() - fireTick) >= firerate*timeLimit) then return end
fireTick = tick()
Fire(player,mousePos,spread)
if Ammoleft.Value <= 0 then return end
spread = 0.5 + (0.25 * Firequeue)
Firequeue += 1
end)
One way to detect when the player stops holding the shooting button down is to use InputEnded event of the UserInputService.
local UserInputService = game:GetService("UserInputService")
local Firequeue = 0
local MaxInaccuracy = 1.0
local InaccuracyIncreasePerShot = 0.01
local InaccuracyDecreasePerSecond = 0.005
local currentInaccuracy = 0.0
local timeLimit = 0.2 -- adjust this as needed
local fireTick = 0
FireEvent.OnServerEvent:Connect(function(player,mousePos)
if equipped == false then return end
if game.Players:GetPlayerFromCharacter(tool.Parent) ~= player then return end
local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
if humanoid.Health <= 0 then return end
if not ((tick() - fireTick) >= firerate*timeLimit) then return end
fireTick = tick()
Fire(player,mousePos,currentInaccuracy)
if Ammoleft.Value <= 0 then return end
currentInaccuracy = math.min(currentInaccuracy + InaccuracyIncreasePerShot, MaxInaccuracy)
spread = 0.5 + (0.25 * Firequeue)
Firequeue += 1
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
currentInaccuracy = 0.0
end
end)
game:GetService("RunService").Heartbeat:Connect(function(dt)
currentInaccuracy = math.max(currentInaccuracy - InaccuracyDecreasePerSecond*dt, 0.0)
end)