The gun won't always reload correctly, it usually reloads correctly, however sometimes it will just say the number of bullets, which is 6. But the gun won't shoot anymore.
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local debounce = false
local bullets = tool:WaitForChild("Bullets")
local reloading = false
local contextActionService = game:GetService("ContextActionService")
--Settings
local studDistance = 100
local cooldown = 1
local reloadtime = 3
tool.Equipped:Connect(function(mouse)
game.ReplicatedStorage.Guns.Pistol.Animations.AnimEvent:FireServer(script.Parent)
mouse.Icon = "http://www.roblox.com/asset?id=936803874"
mouse.Button1Down:Connect(function()
if reloading then
wait(1)
else
if debounce == false then
debounce = true
if bullets.Value > 0 then
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * studDistance)
local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
bullets.Value = bullets.Value - 1
print(bullets.Value)
game.ReplicatedStorage.Guns.Pistol.PistolEvent:FireServer(tool,position,part)
wait(cooldown)
print(ray)
debounce = false
end
end
end
end)
end)
tool.Unequipped:Connect(function()
game.ReplicatedStorage.Guns.Pistol.Animations.AnimEventSTOP:FireServer(script.Parent)
mouse.Icon = ""
end)
function reload()
reloading = true
game.ReplicatedStorage.Guns.Pistol.Animations.ReloadAnim:FireServer()
game.Players.LocalPlayer.PlayerGui.Guns.PistolK1.BulletsGui.BulletsAmount.Text = "Reloading"
wait(reloadtime)
bullets.Value = 6
reloading = false
end
local reloadMobileButton = contextActionService:BindAction("ReloadBtn",reload,true,"r")
contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419")
Here is the bullet change script.
script.Parent.Equipped:Connect(function(mouse)
game.Players.LocalPlayer.PlayerGui.Guns.PistolK1.BulletsGui.BulletsAmount.Visible = true
game.Players.LocalPlayer.PlayerGui.Guns.PistolK1.BulletsGui.BulletsAmount.Text = "Bullets:"..script.Parent.Bullets.Value
end)
script.Parent.Bullets.Changed:Connect(function(bullets)
print(bullets)
game.Players.LocalPlayer.PlayerGui.Guns.PistolK1.BulletsGui.BulletsAmount.Text = "Bullets:"..script.Parent.Bullets.Value
end)
script.Parent.Unequipped:Connect(function(mouse)
game.Players.LocalPlayer.PlayerGui.Guns.PistolK1.BulletsGui.BulletsAmount.Visible = false
end)
