Can someone explain to me why my Gun is always reloading first when I equipp it?

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
1 Like

Please move this to #help-and-feedback:scripting-support

Code Review is for improving working code, not fixing/debugging code. Please read About the Code Review category

3 Likes

Can we se how it’s fired? (how the functions are fired)

You mean this?

Tool.Equipped:Connect(OnEquipped)

Yes, but how is the other reload event fired?

So my reload function is being fired with this line of code
EDIT: you maybe need to scroll to the right

	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")

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")

The problem with that is that the mobile button will be always visible now, even if I don’t have the weapon equipped

then you can simply fix it like this:

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")
1 Like

That made the trick :))
very simple if I think about it, thanks!

1 Like