Gun System not working for Xbox

I made a Gun System in my game, and It works on PC, but not Xbox. I thought it was set up right, but it’s not working. No errors in the Output or Console. Let me know what is wrong.

Client (LocalScript:)

wait(0.1)

local anim1 = Instance.new("Animation")
anim1.AnimationId = "rbxassetid://10047693743" --Hold

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://10047698940" --Fire

local anim3 = Instance.new("Animation")
anim3.AnimationId = "rbxassetid://10047691350" --Reload

local anim4 = Instance.new("Animation")
anim4.AnimationId = "rbxassetid://9723155799" --Safe Mode

local track1
local track2
local track3
local track4

local runservice = game:GetService('RunService')
local uis = game:GetService('UserInputService')
local camera = game.Workspace.CurrentCamera
local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()
local configuration = require(script.Parent.Settings)
local ammo = configuration.Ammo
local mag = configuration.Mag
local ammogui = script.Ammogui
local db = false
local shooting = false
local safemode = false
local oldmouse

equipped = false

-----------------Functions-----------------

local function UpdateGui()
	ammogui.TextLabel.Text = ammo..' / '..mag
	ammogui.GunName.Text = configuration.WeaponName
end

local function CameraRecoil()	
	runservice.RenderStepped:Wait()
	local randomy = (math.random(0,configuration.Recoil*2)-configuration.Recoil)/1200

	if game.Workspace.CurrentCamera.CFrame.LookVector.Y < 0.9 then
		game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame*CFrame.Angles(configuration.Recoil/360/2,randomy/4,math.random(-0.9,0.9))
		runservice.RenderStepped:Wait()
		game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame*CFrame.Angles(configuration.Recoil/360/2,randomy/4,math.random(-0.9,0.9))
	else
		runservice.RenderStepped:Wait()
	end

	local value = 0
	for i=0.01,1,-1 do
		game["Run Service"].RenderStepped:Wait()
	game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame*CFrame.Angles(-configuration.Recoil/7200*i/14,randomy/40*i/10,math.random(-0.9,0.9))
		value = value + i/10
	end
	return true

end

-----------------Shoot-----------------

uis.InputBegan:Connect(function(input, keyBoard)
	if configuration.Auto == true then
		if equipped == true then
			if input.UserInputType == Enum.UserInputType.MouseButton1 or input.KeyCode == Enum.KeyCode.ButtonR2 then
				if shooting == false then
					if safemode == false then
						if db == false then
							if ammo > 0 then
								shooting = true
								repeat
									db = true
									ammo = ammo - 1
									mouse.TargetFilter = workspace.Bullets
									script.Parent.Shot:FireServer(mouse.Hit.p)
									track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
									track2.Priority = Enum.AnimationPriority.Action2
									track2.Looped = false
									track2:Play()
									CameraRecoil()
									UpdateGui()
									task.wait(.01)
									wait(configuration.FireRate)
									db = false
								until shooting == false or ammo == 0 or equipped == false or safemode == true
							end
						end
					end
				end
			end
		end
	else
		if equipped == true then
			if input.UserInputType == Enum.UserInputType.MouseButton1 or input.KeyCode == Enum.KeyCode.ButtonR2 then
				if shooting == false then
					if safemode == false then
						if db == false then
							if ammo > 0 then
								db = true
								ammo = ammo - 1
								mouse.TargetFilter = workspace.Bullets
								script.Parent.Shot:FireServer(mouse.Hit.p)
								track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
								track2.Priority = Enum.AnimationPriority.Action2
								track2.Looped = false
								track2:Play()
								CameraRecoil()
								UpdateGui()
								task.wait(.01)
								wait(configuration.FireRate)
								db = false
							end
						end
					end
				end
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, keyBoard)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.KeyCode == Enum.KeyCode.ButtonR2 then
		if shooting then
			shooting = false
		end
	end
end)

-----------------Reload-----------------

uis.InputBegan:Connect(function(input)
	if input.KeyCode == configuration.ReloadKey then
		if equipped == true then
			if safemode == false then
				track3 = script.Parent.Parent.Humanoid:LoadAnimation(anim3)
				track3.Priority = Enum.AnimationPriority.Action
				track3.Looped = false
				track3:Play()
				ammogui.TextLabel.Text = 'Reloading'
				equipped = false
				script.Parent.Handle.Reload:Play()
				wait(configuration.ReloadTime)
				equipped = true
				ammo = mag
				UpdateGui()
			end
		end
	end
end)

-----------------Safe Mode-----------------

uis.InputBegan:Connect(function(input)
	if configuration.SafeMode == true then
		if input.KeyCode == configuration.SafeModeKey then
			if db == false then
				if equipped == true then
					if safemode == false then
						safemode = true
						track4 = script.Parent.Parent.Humanoid:LoadAnimation(anim4)
						track4.Priority = Enum.AnimationPriority.Action
						track4.Looped = true
						track4:Play()
					else
						db = true
						safemode = false
						if track4 then
							track4:Stop()
						end
						wait(0.3)
						db = false
					end
				end
			end
		end
	end
end)

-----------------Equip Unequipped-----------------

script.Parent.Equipped:Connect(function()
	db = true
	equipped = true
	ammogui.Parent = player.PlayerGui
	UpdateGui()
	script.Parent.EquippingValue.Value = true

	oldmouse = mouse.Icon
	mouse.Icon = configuration.MouseIcon

	track1 = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track1.Priority = Enum.AnimationPriority.Action
	track1.Looped = true
	track1:Play()
	script.Parent.Handle.Equip:Play()
	wait(configuration.EquipTime)
	db = false
end)

script.Parent.Unequipped:Connect(function()
	ammogui.Parent = script
	mouse.Icon = oldmouse
	equipped = false
	safemode = false
	if track1 then
		track1:Stop()
	end
	if track2 then
		track2:Stop()
	end
	if track3 then
		track3:Stop()
	end
	if track4 then
		track4:Stop()
	end
	
	script.Parent.EquippingValue.Value = false
end)

Server (Script:)

local configuration = require(script.Parent.Settings)
local debris = game:GetService('Debris')

equipped = false
script.Parent.Equipped:Connect(function()
	equipped = true
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
end)

----------dont change-----------
local fired = false
local firedTime = 0
local eventTime = 0
local fireRate = 0
--------------------------------

function countCheck()
	if firedTime-eventTime >= fireRate or fireRate-(firedTime-eventTime) <= 1/60 then
		return true
	end
end
script.Parent.Shot.OnServerEvent:Connect(function(player,mousePos)
	if not player.Character == script.Parent.Parent then return end
	
	firedTime = tick()
	if equipped and countCheck() then
		eventTime = tick()
		script.Parent.Handle.Fire:Play()
		script.Parent.Handle.Attachment.PointLight.Enabled = true
		script.Parent.Handle.Attachment.MuzzleFlash.Enabled = true
		script.Parent.Handle.Attachment.MuzzleSmoke.Enabled = true
		for bulletCount = 1, configuration.Bullets do
			task.spawn(fire,player,mousePos)
		end
		wait(0.05)
		script.Parent.Handle.Attachment.MuzzleFlash.Enabled = false
		script.Parent.Handle.Attachment.MuzzleSmoke.Enabled = false
	elseif fired == false then
		fired = true
		task.wait(fireRate-(firedTime-eventTime))
		fired = false
	end
end)

function newBullet()
	local bullet = game.ReplicatedStorage.GunSystem.Bullet:Clone()
	bullet.Position = script.Parent.Fire.Position
	bullet.CanCollide = false
	bullet.CastShadow = false
	bullet.Size = Vector3.new(1,1,1)
	bullet.Name = "bullet"
	bullet.Transparency = 1
	bullet.Parent = workspace.Bullets
	bullet:SetNetworkOwner(nil)
	return bullet
end

function changeGravity(bullet,mass)
	local lift = Instance.new("VectorForce",bullet)
	lift.Force = Vector3.new(0,mass*game.Workspace.Gravity,0) --gravity
	local attachment = Instance.new("Attachment",bullet)
	lift.RelativeTo = Enum.ActuatorRelativeTo.World
	lift.ApplyAtCenterOfMass = true
	lift.Attachment0 = attachment
end

function applyPhysics(bullet,mousePos)
	local muzzleVelocity = configuration.BulletVelocity
	local direction = CFrame.new(bullet.Position,mousePos)
	
	local verticalSpread = configuration.Spread
	local horizontalSpread = configuration.Spread
	local rng = Random.new()
	verticalSpread = rng:NextNumber(-verticalSpread,verticalSpread)
	horizontalSpread = rng:NextNumber(-horizontalSpread,horizontalSpread)
								
	verticalSpread = math.rad(verticalSpread)
	horizontalSpread = math.rad(horizontalSpread)
	direction = direction*CFrame.Angles(verticalSpread,horizontalSpread,0)
	direction = direction.LookVector
	
	local mass = bullet:GetMass()
	bullet:ApplyImpulse(direction * mass * muzzleVelocity)
	
	changeGravity(bullet,mass)
end

function rayFilter(player)
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {player.Character,workspace.Bullets,workspace.NoCollide}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist
	return rayFilter
end

function updateTracer(tracer,lastPos,rayPos)
	tracer.Size = Vector3.new(.1, .1, (lastPos-rayPos).magnitude)
	tracer.CFrame = CFrame.new(rayPos,lastPos)*CFrame.new(0, 0, -(lastPos-rayPos).magnitude/2)
end

function shellejection()
	if configuration.ShellType == 1 then
		local shell = game.ReplicatedStorage.GunSystem.Shell1:Clone()
		shell.CFrame =  script.Parent.Ejection.CFrame
		shell.Velocity = script.Parent.Ejection.CFrame.lookVector * 26 + Vector3.new(0, 5, 0)
		shell.RotVelocity = Vector3.new(-10,40,30)
		shell.CanCollide = false
		shell.Parent = game.Workspace.Shells
	elseif configuration.ShellType == 2 then
		local shell = game.ReplicatedStorage.GunSystem.Shell2:Clone()
		shell.CFrame =  script.Parent.Ejection.CFrame
		shell.Velocity = script.Parent.Ejection.CFrame.lookVector * 26 + Vector3.new(0, 5, 0)
		shell.RotVelocity = Vector3.new(-10,40,30)
		shell.CanCollide = false
		shell.Parent = game.Workspace.Shells
	elseif configuration.ShellType == 3 then
		local shell = game.ReplicatedStorage.GunSystem.Shell3:Clone()
		shell.CFrame =  script.Parent.Ejection.CFrame
		shell.Velocity = script.Parent.Ejection.CFrame.lookVector * 26 + Vector3.new(0, 5, 0)
		shell.RotVelocity = Vector3.new(-10,40,30)
		shell.CanCollide = false
		shell.Parent = game.Workspace.Shells
	end
end

function hitDetection(hit,ray, mousepos)
	local model = hit:FindFirstAncestorOfClass("Model")
	if model then
		local humanoid = model:FindFirstChildWhichIsA("Humanoid") or nil
		if humanoid then
			if hit.Name == "Head" then
				humanoid:TakeDamage(configuration.HeadShotDamage)
			else
				humanoid:TakeDamage(configuration.Damage)
			end
		else
			return true
		end
	end
end

function fire(player,mousePos)
	local bullet = newBullet()
	applyPhysics(bullet,mousePos)
	shellejection()
	
	local rayFilter = rayFilter(player)
	
	local lastPos = bullet.Position
	local range = configuration.MaxRange
	while range > 0 do
		task.wait()

		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength
		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos,rayPos).lookVector
		
		--updateTracer(tracer,lastPos,rayPos)

		local ray = workspace:Raycast(lastPos,rayDirection*rayLength,rayFilter) 
		if ray then
			bullet:Destroy()
			local hit = ray.Instance
			rayPos = ray.Position

			--updateTracer(tracer,lastPos,rayPos)
			
			if hitDetection(hit,ray, mousePos) then
				script.Parent.Shot:FireClient(player)
			end
			break
		end
		if bullet.Parent == nil then
			bullet:Destroy()
			break
		end
		lastPos = bullet.Position
	end
	if bullet then
		bullet:Destroy()
	end
end

--elseif material == Enum.Material.Wood or material == Enum.Material.WoodPlanks then
--	local mat = game.ReplicatedStorage.GunSystem.Impact.Wood
--	if mat then
--		local hole = Instance.new("Part", workspace)
--		local particles = mat:FindFirstChildWhichIsA("BasePart")
--		local tab = mat.Sounds:GetChildren()
--		local rs = tab[math.random(1, #tab)]
--		local cl = rs:Clone()
--		cl.Parent = hole
--		cl:Play()

--		hole.Size = Vector3.new(0.7, 0.7, 0.001)
--		hole.Transparency = 1
--		hole.CanCollide = false
--		hole.Position = pos
--		hole.CFrame = CFrame.new(pos,pos+normal)
--		hole.Anchored = true
--		local decal = mat:FindFirstChildOfClass("Decal")
--		if decal then
--			local clone = decal:Clone()
--			clone.Parent = hole
--		end
--		game.Debris:AddItem(hole, 6)
--	end
--end

Settings (Module Script:)

local module = {
	----Main
	['WeaponName'] = 'Pistol';
	['ShellType'] = 3; --1 rifle, 2 shotgun, 3 pistol
	['Auto'] = false;
	['Ammo'] = 8; --8 default
	['Mag'] = 8; --8 default
	['Bullets'] = 1; --1 default
	['Spread'] = 1; --1 default
	['BulletVelocity'] = 1000; --1000 default
	['FireRate'] = 0.15; --0.15 default
	['Damage'] = math.random(30,35); --30 default
	['HeadShotDamage'] = 50; --50 default
	['MaxRange'] = 2000; --2000 default
	['MouseIcon'] = 'http://www.roblox.com/asset/?id=5681283745';
	['ReloadTime'] = 2.1; --2.1 default
	['Recoil'] = 6; --6 default
	['EquipTime'] = 0.5; --0.5 default
	----Mods
	['SafeMode'] = false;
	----Keys
	['ReloadKey'] = Enum.KeyCode.R; Enum.KeyCode.ButtonY;
	['SafeModeKey'] = Enum.KeyCode.V;
}

return module

Please help soon,

papahetfan

i believe you can replace

['ReloadKey'] = Enum.KeyCode.R; Enum.KeyCode.ButtonY; <-- you're basically putting another index/value with ; after Enum.KeyCode.R

with

['ReloadKey'] = Enum.KeyCode.R and Enum.KeyCode.ButtonY;

or

['ReloadKey'] = Enum.KeyCode.R or Enum.KeyCode.ButtonY;

you’re basically making another index/value on the dictionary instead of putting two values in one dictionary index

So it will fire the weapon, which is good. But the ButtonY Button also fires, which ButtonY should only reload. How could I fix that?

I also changed the script a tiny bit, so hopefully nothing bad is happening.

Client:

wait(0.1)

local anim1 = Instance.new("Animation")
anim1.AnimationId = "rbxassetid://10047693743" --Hold

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://10047698940" --Fire

local anim3 = Instance.new("Animation")
anim3.AnimationId = "rbxassetid://10047691350" --Reload

local anim4 = Instance.new("Animation")
anim4.AnimationId = "rbxassetid://9723155799" --Safe Mode

local track1
local track2
local track3
local track4

local runservice = game:GetService('RunService')
local uis = game:GetService('UserInputService')
local camera = game.Workspace.CurrentCamera
local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()
local configuration = require(script.Parent.Settings)
local ammo = configuration.Ammo
local mag = configuration.Mag
local ammogui = script.Ammogui
local db = false
local shooting = false
local safemode = false
local oldmouse

equipped = false

-----------------Functions-----------------

local function UpdateGui()
	ammogui.TextLabel.Text = ammo..' / '..mag
	ammogui.GunName.Text = configuration.WeaponName
end

local function CameraRecoil()	
	runservice.RenderStepped:Wait()
	local randomy = (math.random(0,configuration.Recoil*2)-configuration.Recoil)/1200

	if game.Workspace.CurrentCamera.CFrame.LookVector.Y < 0.9 then
		game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame*CFrame.Angles(configuration.Recoil/360/2,randomy/4,math.random(-0.9,0.9))
		runservice.RenderStepped:Wait()
		game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame*CFrame.Angles(configuration.Recoil/360/2,randomy/4,math.random(-0.9,0.9))
	else
		runservice.RenderStepped:Wait()
	end

	local value = 0
	for i=0.01,1,-1 do
		game["Run Service"].RenderStepped:Wait()
	game.Workspace.CurrentCamera.CFrame = game.Workspace.CurrentCamera.CFrame*CFrame.Angles(-configuration.Recoil/7200*i/14,randomy/40*i/10,math.random(-0.9,0.9))
		value = value + i/10
	end
	return true

end

-----------------Shoot-----------------

uis.InputBegan:Connect(function(input, keyBoard)
	if configuration.Auto == true then
		if equipped == true then
			if configuration.FireKeyCode then
				if shooting == false then
					if safemode == false then
						if db == false then
							if ammo > 0 then
								shooting = true
								repeat
									db = true
									ammo = ammo - 1
									mouse.TargetFilter = workspace.Bullets
									script.Parent.Shot:FireServer(mouse.Hit.p)
									track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
									track2.Priority = Enum.AnimationPriority.Action2
									track2.Looped = false
									track2:Play()
									CameraRecoil()
									UpdateGui()
									task.wait(.01)
									wait(configuration.FireRate)
									db = false
								until shooting == false or ammo == 0 or equipped == false or safemode == true
							end
						end
					end
				end
			end
		end
	else
		if equipped == true then
			if configuration.FireKeyCode then
				if shooting == false then
					if safemode == false then
						if db == false then
							if ammo > 0 then
								db = true
								ammo = ammo - 1
								mouse.TargetFilter = workspace.Bullets
								script.Parent.Shot:FireServer(mouse.Hit.p)
								track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
								track2.Priority = Enum.AnimationPriority.Action2
								track2.Looped = false
								track2:Play()
								CameraRecoil()
								UpdateGui()
								task.wait(.01)
								wait(configuration.FireRate)
								db = false
							end
						end
					end
				end
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, keyBoard)
	if configuration.FireKeyCode then
		if shooting then
			shooting = false
		end
	end
end)

-----------------Reload-----------------

uis.InputBegan:Connect(function(input)
	if input.KeyCode == configuration.ReloadKey then
		if equipped == true then
			if safemode == false then
				track3 = script.Parent.Parent.Humanoid:LoadAnimation(anim3)
				track3.Priority = Enum.AnimationPriority.Action
				track3.Looped = false
				track3:Play()
				ammogui.TextLabel.Text = 'Reloading'
				equipped = false
				script.Parent.Handle.Reload:Play()
				wait(configuration.ReloadTime)
				equipped = true
				ammo = mag
				UpdateGui()
			end
		end
	end
end)

-----------------Safe Mode-----------------

uis.InputBegan:Connect(function(input)
	if configuration.SafeMode == true then
		if input.KeyCode == configuration.SafeModeKey then
			if db == false then
				if equipped == true then
					if safemode == false then
						safemode = true
						track4 = script.Parent.Parent.Humanoid:LoadAnimation(anim4)
						track4.Priority = Enum.AnimationPriority.Action
						track4.Looped = true
						track4:Play()
					else
						db = true
						safemode = false
						if track4 then
							track4:Stop()
						end
						wait(0.3)
						db = false
					end
				end
			end
		end
	end
end)

-----------------Equip Unequipped-----------------

script.Parent.Equipped:Connect(function()
	db = true
	equipped = true
	ammogui.Parent = player.PlayerGui
	UpdateGui()
	script.Parent.EquippingValue.Value = true

	oldmouse = mouse.Icon
	mouse.Icon = configuration.MouseIcon

	track1 = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track1.Priority = Enum.AnimationPriority.Action
	track1.Looped = true
	track1:Play()
	script.Parent.Handle.Equip:Play()
	wait(configuration.EquipTime)
	db = false
end)

script.Parent.Unequipped:Connect(function()
	ammogui.Parent = script
	mouse.Icon = oldmouse
	equipped = false
	safemode = false
	if track1 then
		track1:Stop()
	end
	if track2 then
		track2:Stop()
	end
	if track3 then
		track3:Stop()
	end
	if track4 then
		track4:Stop()
	end
	
	script.Parent.EquippingValue.Value = false
end)

Settings (Module Script:)

local module = {
	----Main
	['WeaponName'] = 'Pistol';
	['FireKeyCode'] = Enum.UserInputType.MouseButton1 or Enum.KeyCode.ButtonR2;
	['ShellType'] = 3; --1 rifle, 2 shotgun, 3 pistol
	['Auto'] = false;
	['Ammo'] = 8; --8 default
	['Mag'] = 8; --8 default
	['Bullets'] = 1; --1 default
	['Spread'] = 1; --1 default
	['BulletVelocity'] = 1000; --1000 default
	['FireRate'] = 0.15; --0.15 default
	['Damage'] = math.random(30,35); --30 default
	['HeadShotDamage'] = 50; --50 default
	['MaxRange'] = 2000; --2000 default
	['MouseIcon'] = 'http://www.roblox.com/asset/?id=5681283745';
	['ReloadTime'] = 2.1; --2.1 default
	['Recoil'] = 6; --6 default
	['EquipTime'] = 0.5; --0.5 default
	----Mods
	['SafeMode'] = false;
	----Keys
	['ReloadKey'] = Enum.KeyCode.R or Enum.KeyCode.ButtonY;
	['SafeModeKey'] = Enum.KeyCode.V;
}

return module

did you check if the input is equal to the fire/reload keycodes?

Not getting any error’s in the Output or Console. Everytime the xbox player equips, it shoots. It can shoot, when ButtonR2 is pressed, but ButtonY does not work.

That’s not how tables work. You can’t store multiple values with one key, unless you store them in another table.

This will set ReloadKey to Enum.KeyCode.R and then create a new index with the value of Enum.Keycode.ButtonY.

['ReloadKey'] = Enum.KeyCode.R; Enum.KeyCode.ButtonY;

Using and in this situation will return Enum.KeyCode.ButtonY, if my memory is correct.

['ReloadKey'] = Enum.KeyCode.R and Enum.KeyCode.ButtonY;

Using or in this situation will return Enum.KeyCode.R, ignoring Enum.KeyCode.ButtonY entirely.

['ReloadKey'] = Enum.KeyCode.R or Enum.KeyCode.ButtonY;

In order to get the desired outcome, you could do something like this and iterate on the table, checking if the user input matches any of those values.

['ReloadKey'] = {Enum.KeyCode.R; Enum.KeyCode.ButtonY};

You could also create a new module specifically for console players. require this new module if the player is playing on a console and use the entries to override the same entries in your settings module.

For instance, your normal settings module would have

['ReloadKey'] = Enum.KeyCode.R;

While your console module would have

['ReloadKey'] = Enum.KeyCode.ButtonY;

And using a simple loop, like so, you would be able to replace keyboard specific values with console specific values.

for i,v in pairs (consoleConfiguration) do
	if configuration[i] then
		configuration[i] = v
	end
end

I made a different Module Script for the console players, and so far it works on computer, but I can’t test on console yet. But I’ll let you know if it works.