i forgot to include one more thing that is probablly important, so i already have a script that if i click r then it reloads,
userinputservice.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keycode = input.KeyCode
if keycode == Enum.KeyCode.R and not reloading then
reloading = true
wait(cooldown) -- Add a cooldown before allowing shooting again after reloading
gun.Ammo.Value = 15
player:FindFirstChild("ParticleEmitter").Parent = gun.Smoke
player:FindFirstChild("BillboardGui").Parent = gun.Flash
gun.Magazine.Value = gun.Magazine.Value - gun.Ammo.Value
reloading = false
end
end
end
end)
Your system has lots of flaws, but this should work.
local UIS = game:GetService("UserInputService")
local function reloadGun()
reloadSound:Play()
reloadAnimation:Play()
gun.Ammo.Value = -- max ammo
end
gun.Activated:Connect(function()
if gun.Ammo.Value == 0 then
reloadGun()
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R and equipped then
reloadGun()
end
end)
Okay let me explain a few things, im trying to make a hood game and im trying to add a switch to it (basically an automatic pistol) it was automatic before, but after i tried your script and then deleted it for testing it’s just a semi. Could ya help?
Pretty basic scripts. You should modify them based on your needs.
Local Script:
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local gunTool = script.Parent
local handle = gunTool:WaitForChild("Handle")
local player = Players.LocalPlayer
local character = player.Character
local reloadButton = Enum.KeyCode.R
local reloadSound = handle.Reload
local isShooting = false
local isReloading = false
local shotCooldown = false
local shotDelay = 0.1
local shootEvent = gunTool.ShootEvent
local reloadEvent = gunTool.ReloadEvent
gunTool.Equipped:Connect(function()
UIS.InputBegan:Connect(function(input)
if input.KeyCode == reloadButton and not isShooting then
if not isReloading then
reloadEvent:FireServer()
isReloading = true
wait(reloadSound.TimeLength)
isReloading = false
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not isReloading then
if not isShooting then
isShooting = true
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isShooting = false
end
end)
end)
RunService.RenderStepped:Connect(function()
if isShooting and not shotCooldown then
shotCooldown = true
shootEvent:FireServer(character)
wait(shotDelay)
shotCooldown = false
end
end)
Server Script:
local gunTool = script.Parent
local handle = gunTool:WaitForChild("Handle")
local effectsAttachment = handle:WaitForChild("Attachment")
local maxAmmo = 17
local currentAmmo = Instance.new("NumberValue")
currentAmmo.Name = "CurrentAmmo"
currentAmmo.Parent = gunTool
currentAmmo.Value = maxAmmo
local shootSound = handle.Shoot
local emptySound = handle.Empty
local reloadSound = handle.Reload
local shootEvent = gunTool.ShootEvent
local reloadEvent = gunTool.ReloadEvent
reloadEvent.OnServerEvent:Connect(function(player, character)
reloadSound:Play()
wait(reloadSound.TimeLength)
currentAmmo.Value = maxAmmo
end)
shootEvent.OnServerEvent:Connect(function(player, character)
local humanoid = character:WaitForChild("Humanoid")
local recoilTrack = humanoid:LoadAnimation(handle.Recoil)
if currentAmmo.Value > 0 then
print("shot")
shootSound:Play()
currentAmmo.Value = currentAmmo.Value - 1
for _, effects in pairs(effectsAttachment:GetChildren()) do
if effects:IsA("ParticleEmitter") or effects:IsA("PointLight") then
effects.Enabled = true
wait(0.1)
effects.Enabled = false
end
end
recoilTrack:Play()
else
emptySound:Play()
end
end)