Roblox advanced shooting script.
unknown
lua
3 years ago
2.2 kB
16
Indexable
-- Define constants
local MAX_SHOTS_PER_SECOND = 10 -- Maximum number of shots allowed per second
local SHOT_DAMAGE = 10 -- Amount of damage each shot does
local SHOT_RANGE = 500 -- Maximum distance a shot can travel
local SHOT_SPEED = 100 -- Speed at which shots travel
-- Define variables
local lastShotTime = 0 -- Time (in seconds) of last shot fired
local canShoot = true -- Whether or not the player can currently shoot
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
-- Function to handle firing the gun
function fireGun()
if canShoot then
-- Calculate time since last shot and check if enough time has passed
local currentTime = tick()
local timeSinceLastShot = currentTime - lastShotTime
local shotsPerSecond = 1 / timeSinceLastShot
if shotsPerSecond <= MAX_SHOTS_PER_SECOND then
-- Fire the gun
local origin = player.Character.Head.Position
local direction = (mouse.Hit.Position - origin).Unit
local distance = math.min((mouse.Hit.Position - origin).Magnitude, SHOT_RANGE)
local bullet = Instance.new("Part")
bullet.Anchored = true
bullet.CanCollide = false
bullet.Size = Vector3.new(0.2, 0.2, 1)
bullet.Position = origin
bullet.CFrame = CFrame.new(origin, origin + direction * distance)
bullet.Parent = game.Workspace
local bulletVelocity = bullet.CFrame.LookVector * SHOT_SPEED
bullet.Velocity = bulletVelocity
game:GetService("Debris"):AddItem(bullet, 5)
lastShotTime = currentTime
-- Handle hit detection and damage
local hitPart, hitPosition = workspace:FindPartOnRay(Ray.new(origin, direction * distance))
if hitPart then
local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(SHOT_DAMAGE)
end
end
end
end
end
-- Connect firing function to mouse click
mouse.Button1Down:Connect(fireGun)
Editor is loading...