Heya everyone.
I’m working on a simple gun for a fan recreation of a game called “Noobs vs Zombies: Relish Reborn” I’m working on. Everything almost works aside from the reloading function. For some reason, binding the reload function immediately causes it to fire or in other words, cause the user to reload.
--//Tool
local Tool = script.Parent
local FireEvent = Tool:WaitForChild("FireEvent")
local AnimationFolder = Tool:WaitForChild("Animations")
--Attributes
local AmmoHold = Tool:GetAttribute("Ammo_Hold")
local AmmoContain = Tool:GetAttribute("Ammo_Contain")
local Cooldown = Tool:GetAttribute("Cooldown")
local FullyEquipped = Tool:GetAttribute("FullyEquipped")
--//Functionality
local CAS = game:GetService("ContextActionService")
--Getting player's mouse
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
--//Reloading
local ReloadCD = false
local function Reloading(actionName, inputState, inputObj)
if (inputState == Enum.UserInputState.Begin) then
local ammoContain = Tool:GetAttribute("Ammo_Contain")
local ammoHold = Tool:GetAttribute("Ammo_Hold")
if not ReloadCD then
ReloadCD = true
--//Checking to see if AmmoHold is less.
if Tool:GetAttribute("Ammo_Hold") <= AmmoHold then
--//We're also checking to see if they have enough ammo to even reload it.
if Tool:GetAttribute("Ammo_Contain") >= 1 then
--//Reloading Animation
local ReloadAnim = Player.Character.Humanoid:FindFirstChild("Animator"):LoadAnimation(AnimationFolder.Reload)
ReloadAnim:Play()
ReloadAnim:GetMarkerReachedSignal("RackedGunIndication"):Connect(function()
Tool.Pistol.Reload:Play()
end)
ReloadAnim.Stopped:Wait()
task.wait(0.5)
Tool:SetAttribute("Ammo_Hold", 12)
Tool:SetAttribute("Ammo_Contain", ammoContain - 12)
FireEvent:FireServer("Reload")
print("Ammo Remaining: "..ammoContain.."... Better use it wisely.")
ReloadCD = false
else
warn("You don't got ammo!! Go near an ammo box or find an ammo noob to refill!!")
ReloadCD = false
return
end
else
print("Your gun is already loaded! Silly!!")
ReloadCD = false
return
end
end
return Enum.ContextActionResult.Pass
end
end
------------------------------------------------------------------------
--//Firing the weapon.
local CD = false
Tool.Activated:Connect(function()
if Tool:GetAttribute("FullyEquipped") == false then
return
end
if not CD then
CD = true
local ammoHold = Tool:GetAttribute("Ammo_Hold")
if ammoHold > 0 then
Tool.FireEvent:FireServer("Fire", Mouse.Hit.Position)
Tool:SetAttribute("Ammo_Hold", Tool:GetAttribute("Ammo_Hold") - 1)
--//Firing Animation
local FiringAnim = Player.Character.Humanoid:FindFirstChild("Animator"):LoadAnimation(AnimationFolder.Fire)
FiringAnim:Play()
else
Reloading()
--[[
print("Ran out of Ammo!! Womp womp.")
Tool.Pistol.NoAmmoNoise:Play()
]]
end
task.wait(Cooldown)
CD = false
end
end)
--//Misc
local IdleAnim = AnimationFolder:FindFirstChild("Idle")
Tool.Equipped:Connect(function()
--
local Coro = coroutine.create(function()
--//Playing equip animation
local EquipAnim = Player.Character.Humanoid:FindFirstChild("Animator"):LoadAnimation(AnimationFolder:FindFirstChild("Equip"))
EquipAnim.Priority = Enum.AnimationPriority.Action4
EquipAnim:Play()
EquipAnim.Stopped:Wait()
Tool:SetAttribute("FullyEquipped", true)
CAS:BindAction("Reload",Reloading,false,Enum.KeyCode.R)
--//Playing idle animation
local IdleAnimTrack = Player.Character.Humanoid:FindFirstChild("Animator"):LoadAnimation(IdleAnim)
IdleAnimTrack:Play()
Tool.Unequipped:Connect(function()
IdleAnimTrack:Stop()
end)
end)
coroutine.resume(Coro)
Tool.Unequipped:Connect(function()
coroutine.close(Coro)
CAS:UnbindAction("Reload")
Tool:SetAttribute("FullyEquipped", false)
end)
end)