My gun auto local script keeps changing each time i test my game

local jammed = false
ismobile = userInputService.TouchEnabled
iscomputer = userInputService.KeyboardEnabled


-- Reload Function
local function reload ()
	if reloading == false then
		reloading = true
		ReloadAnim = tool:WaitForChild("Reload")
		ReloadTrack = hum:LoadAnimation(ReloadAnim)
		ReloadTrack:Play()
		local part = script.Parent.Handle.mag:Clone()
		part.Position = tool.Handle.mag.Position
		part.CFrame = tool.Handle.CFrame * CFrame.Angles(0, math.rad(90), 0)
		part.Parent = workspace
		part.CanCollide = true
		part.BrickColor = BrickColor.new("Smoky grey")
		part.Material = Enum.Material.Metal
		script.Parent.Handle["Gun Reload"]:Play()
		script.Parent.Handle.mag.Transparency = 1
		tool.Parent:WaitForChild("run?").Value = false
		tool.Parent:WaitForChild("Humanoid").WalkSpeed = 10
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
		task.wait(3)
		local random = math.random(1,2)
		if random == 1 then
			print("gunnotjammed")
		else
			jammed = true
			script.Parent.Handle.JAMMED:Play()
			print("gungotjammed")
		end
		tool.Parent:WaitForChild("run?").Value = true
		tool.Parent:WaitForChild("Humanoid").WalkSpeed = 17
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		script.Parent.Handle.mag.Transparency = 0
		ammo = maxAmmo
		player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
		player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
		if ReloadTrack then
			ReloadTrack:Stop()
		end
		reloading = false
		wait(4)
		part:Destroy()
	end
end


-- Shooting function

local function shoot()
if iscomputer then
	if not tool:IsDescendantOf(player.Character) then return end
	local Time = tick()
	if Time - lastShot >= shotDelay then
		lastShot = Time
		if debounce == false and ammo > 0 and reloading == false and holding == true and jammed == false then
			debounce = true 
			local maths = math.random(5,7)
			local maf = math.random(1,2)
			ammo -= 1
			tool.Shoot:FireServer(mouse.Hit.Position)
			if shotanimtrack then
				shotanimtrack:Play()
			end
			if maf == 1 then
				script.Parent.Handle.Barrel["Gun shot"]:Play()
				script.Parent.Handle.Barrel["Gun shot"].Volume = maths
				print(maths)
			elseif maf == 2 then
				script.Parent.Handle.Barrel["Gun shot2"]:Play()
				script.Parent.Handle.Barrel["Gun shot2"].Volume = maths
				print(maths)
			end
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(2),0,0)
			player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
			player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
			task.wait(0.09)
			debounce = false
		elseif ammo <= 0 and reloading == false then
			reload()
			end
		end
	end
end


local function moblieshooting()
	if ismobile then
		if not tool:IsDescendantOf(player.Character) then return end
		if debounce == false and ammo > 0 and reloading == false and jammed == false then
			debounce = true 
			ammo -= 1
			tool.Shoot:FireServer(mouse.Hit.Position)
			if shotanimtrack then
				shotanimtrack:Play()
			end
			workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(2),0,0)
			player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
			player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
			task.wait(0.09)
			debounce = false
		elseif ammo <= 0 and reloading == false then
			reload()
		end
	end
end

this isn’t the full local script but its where it does the shooting for mobile, i have no idea why this happens tho, like if i touch the screen it only shoots once which i want to happen but if i test the game again and touch the screen it doesn’t shoot anymore for some weird reason there’s no error it just happens

you are calling the variables once, meaning that those are true/false until you re-call those.

what i mean is:

ismobile = true
iscomputer = false

if mobile then
print ("mobile")
end

if mobile then
print ("computer") --this one will never get print because the variable "isComputer" is indefinitely set as false
end

oh, i don’t get what are you trying to say

because you are calling those variables out of any function, they only define what was true before running any code, if you put those inside the function that needs those variables you’ll get updated variables every time this functions runs.

ohhh so i put it in the function so it repeats each time, is that what you mean?

Yes exactly, keeping those variables out of the function only “defines” (if thats the right term) once, while in a function every time said function runs they will be checked.

oh ok, ill try it out later ill replay if it works!

uhhh do i put a local here?

wait never mind that won’t work

Not necessarily, it is actually better if you don’t if you’re checking those variables in different functions too, although if you want to you could use “local”, then you would need to copy and paste the variables for all other functions that you intend to use those variables in.


i have another function to

I’m guessing since they’re both simply for shooting i would keep it as it is.

oh, but what about the orange line tho

That is just a warning saying that you’re only using that variable in a single function, doesnt do anything in your code though so can keep it.

oh alright ill see if this works gimme a minute

uhhh so now my gun doesn’t shoot

bruh, can you send an image of the output?

scroll up, you’re not showing the full output log.