So my gun always reloads when I equipp it and I think it has something to do with the mobilebutton I made via ContextactionService for reloading on mobile because it’s the only thing related to reloading in the equipp function.
Am I right and if so, how could I solve this problem?
function OnEquipped(PlayerMouse)
Mouse = PlayerMouse
ExpectingInput = true
--Cloe UI to player
ui:Clone().Parent = localP.PlayerGui
UpdateMouseIcon()
equipAnimation:FireServer(animIdle)
--Update the UI to current bullets
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = bullets.Value.."/"..CLIP_SIZE
--//handle the reload aswell as mobile relaod
local reloadMobileButton = contextActionService:BindAction("ReloadBtn", Reload, true , "r")
contextActionService:SetPosition("ReloadBtn", UDim2.new(0.5, 0,0, 0))
contextActionService:SetImage("ReloadBtn", "http://www.roblox.com/asset/?id=10952419")
end
My Reload function
function Reload()
if RELOADING then
--//do nothing
else
RELOADING = true
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = "-/-"
wait(RELOAD_TIME)
bullets.Value = CLIP_SIZE
ReloadEvent:FireServer()
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = bullets.Value.."/"..CLIP_SIZE
equipAnimation:FireServer(animIdle)
RELOADING = false
end
end
I am getting the R key there and creating the mobile button aswell. So it fires normaly when I press R ( which totaly works ) but my problem is that everytime I equipp the weapon it reloads once.
Ok, it’s probably because whenever you equip a tool, the script re-activates, meaning it has to find all the variables and fire all the code. In this case, its probably because when the tool is equipped, this line is fired:
local reloadMobileButton = contextActionService:BindAction("ReloadBtn", Reload, true , "r")
that is because you are firing the reload event AND the equipped event at the same time.
So to counteract that you simply move the code elsewhere:
function OnEquipped(PlayerMouse)
Mouse = PlayerMouse
ExpectingInput = true
--Cloe UI to player
ui:Clone().Parent = localP.PlayerGui
UpdateMouseIcon()
equipAnimation:FireServer(animIdle)
--Update the UI to current bullets
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = bullets.Value.."/"..CLIP_SIZE
end
function Reload()
if RELOADING then
--//do nothing
else
RELOADING = true
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = "-/-"
wait(RELOAD_TIME)
bullets.Value = CLIP_SIZE
ReloadEvent:FireServer()
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = bullets.Value.."/"..CLIP_SIZE
equipAnimation:FireServer(animIdle)
RELOADING = false
end
end
--//handle the reload aswell as mobile relaod
local reloadMobileButton = contextActionService:BindAction("ReloadBtn", Reload, true , "r")
contextActionService:SetPosition("ReloadBtn", UDim2.new(0.5, 0,0, 0))
contextActionService:SetImage("ReloadBtn", "http://www.roblox.com/asset/?id=10952419")
function OnEquipped(PlayerMouse)
Mouse = PlayerMouse
ExpectingInput = true
--Cloe UI to player
ui:Clone().Parent = localP.PlayerGui
UpdateMouseIcon()
equipAnimation:FireServer(animIdle)
--Update the UI to current bullets
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = bullets.Value.."/"..CLIP_SIZE
contextActionService:SetPosition("ReloadBtn", UDim2.new(0.5, 0,0, 0))
contextActionService:SetImage("ReloadBtn", "http://www.roblox.com/asset/?id=10952419")
end
function Reload()
if RELOADING then
--//do nothing
else
RELOADING = true
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = "-/-"
wait(RELOAD_TIME)
bullets.Value = CLIP_SIZE
ReloadEvent:FireServer()
localP.PlayerGui:WaitForChild("UI").Frame.Ammo.Text = bullets.Value.."/"..CLIP_SIZE
equipAnimation:FireServer(animIdle)
RELOADING = false
end
end
--//handle the reload aswell as mobile relaod
local reloadMobileButton = contextActionService:BindAction("ReloadBtn", Reload, true , "r")