So, I made a custom gun for players. But it won’t make the bullet for Xbox players and Touchable devices. But it works on all computer players. How can I fix this:
Here is the LocalScript:
ocal tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local maxAmmo = 14
local CoolDown = 0.23
local ammo = maxAmmo
local reloading = false
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
local plrgui = client.PlayerGui
local text = plrgui:WaitForChild("Ammo").AmmoCount
local BoolValue = script.Parent:WaitForChild("EquippedValue")
tool.Equipped:Connect(function()
BoolValue.Value = true
cursor.Icon = "rbxassetid://42445293"
plrgui.Ammo.Enabled = true
text.DisableScript.Disabled = true
plrgui.Shop.Enabled = false
plrgui.Inventory.Enabled = false
local function reload()
reloading = true
tool.Handle.reload:Play()
wait(1)
ammo = maxAmmo
reloading = false
end
cursor.Button2Down:Connect(function()
if BoolValue.Value == true then
game.Workspace.CurrentCamera.FieldOfView = 40
client.Character:WaitForChild("Humanoid").WalkSpeed = 14.5
plrgui:WaitForChild("HotBarGUI").Enabled = false
end
end)
cursor.Button2Up:Connect(function()
if BoolValue.Value == true then
game.Workspace.CurrentCamera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end
end)
tool.Activated:Connect(function()
if ammo > 0 and not reloading then
ammo = ammo - 1
wait(CoolDown)
remote:FireServer(cursor.Hit.Position)
text.Text = (ammo).." / "..tostring(maxAmmo)
tool.Handle.gunshot:Play()
tool.Handle.PointLight.Enabled = true
wait(0.2)
tool.Handle.PointLight.Enabled = false
elseif reloading == false then
reload()
tool.Handle.gunshot:Stop()
end
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
reload()
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.ButtonY then
reload()
end
end)
end)
while wait() do
text.Text = (ammo).." / "..tostring(maxAmmo)
end
end)
script.Parent.Unequipped:Connect(function()
plrgui.Ammo.Enabled = false
text.DisableScript.Disabled = false
plrgui.Shop.Enabled = true
plrgui.HotBarGUI.Enabled = true
plrgui.Inventory.Enabled = true
BoolValue.Value = false
game.Workspace.CurrentCamera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end)
Server Script (Normal Script):
local tool = script.Parent
local shoot_part = tool:WaitForChild("Handle")
local remote = tool:WaitForChild("OnShoot")
local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
remote.OnServerEvent:Connect(function(player, position)
local origin = shoot_part.Position
local direction = (position - origin).Unit*300
local result = Workspace:Raycast(origin, direction)
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = ServerStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
local PlayerName = Instance.new("StringValue")
PlayerName.Name = "Attacker"
PlayerName.Value = player.Name
PlayerName.Parent = bullet_clone
if result then
local part = result.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid.Parent.Name ~= bullet_clone.Attacker.Value then
humanoid:TakeDamage(math.random(10,14))
end
end
wait(0.25)
bullet_clone:Destroy()
end)
If I’m not wrong, Tool.Activated is normally triggered with a mouse. If you want to trigger the tool for other devices, you can use Tool.Activate() in the script, alternatively you can make the firing bullet a function then assign the function to multiple input types.
Replace your LocalScript code with this, it should function correctly.
local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local maxAmmo = 14
local CoolDown = 0.23
local ammo = maxAmmo
local reloading = false
local Camera = game:GetService("Workspace").CurrentCamera
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
local CAS = game:GetService("ContextActionService")
local plrgui = client.PlayerGui
local text = plrgui:WaitForChild("Ammo").AmmoCount
local BoolValue = script.Parent:WaitForChild("EquippedValue")
local ZoomTypes = { Enum.UserInputType.MouseButton2, Enum.KeyCode.ButtonL2 }
local ReloadTypes = { Enum.KeyCode.R, Enum.KeyCode.ButtonX }
local FireTypes = { Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2 }
local function GunHandler(ActionName, inputState, inputObject)
if ActionName == "WeaponZoom" and BoolValue.Value and inputState == Enum.UserInputState.Begin and (table.find(ZoomTypes, inputObject.UserInputType) or table.find(ZoomTypes, inputObject.KeyCode)) then
Camera.FieldOfView = 40
client.Character:WaitForChild("Humanoid").WalkSpeed = 14.5
plrgui:WaitForChild("HotBarGUI").Enabled = false
end
if ActionName == "WeaponZoom" and inputState == Enum.UserInputState.End and (table.find(ZoomTypes, inputObject.UserInputType) or table.find(ZoomTypes, inputObject.KeyCode)) then
Camera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end
if ActionName == "Reload" and BoolValue.Value and table.find(ReloadTypes, inputObject.KeyCode) and not reloading then
reloading = true
tool.Handle.reload:Play()
task.wait(1)
ammo = maxAmmo
reloading = false
end
if ActionName == "FireWeapon" and BoolValue.Value and (table.find(FireTypes, inputObject.UserInputType) or table.find(FireTypes, inputObject.KeyCode)) and not reloading and ammo > 0 then
ammo = ammo - 1
task.wait(CoolDown)
remote:FireServer(cursor.Hit.Position)
text.Text = (ammo).." / "..tostring(maxAmmo)
tool.Handle.gunshot:Play()
tool.Handle.PointLight.Enabled = true
task.wait(0.2)
tool.Handle.PointLight.Enabled = false
elseif ActionName == "FireWeapon" and BoolValue.Value and (table.find(FireTypes, inputObject.UserInputType) or table.find(FireTypes, inputObject.KeyCode)) and not reloading then
reloading = true
tool.Handle.reload:Play()
task.wait(1)
ammo = maxAmmo
reloading = false
tool.Handle.gunshot:Stop()
end
end
CAS:BindAction("FireWeapon", GunHandler, true, Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2)
CAS:BindAction("Reload", GunHandler, true, Enum.KeyCode.R, Enum.KeyCode.ButtonX)
CAS:BindAction("WeaponZoom", GunHandler, true, Enum.UserInputType.MouseButton2, Enum.KeyCode.ButtonL2)
task.spawn(function()
while true do
text.Text = (ammo).." / "..tostring(maxAmmo)
task.wait()
end
end)
tool.Equipped:Connect(function()
BoolValue.Value = true
cursor.Icon = "rbxassetid://42445293"
plrgui.Ammo.Enabled = true
text.DisableScript.Disabled = true
plrgui.Shop.Enabled = false
plrgui.Inventory.Enabled = false
end)
script.Parent.Unequipped:Connect(function()
plrgui.Ammo.Enabled = false
text.DisableScript.Disabled = false
plrgui.Shop.Enabled = true
plrgui.HotBarGUI.Enabled = true
plrgui.Inventory.Enabled = true
BoolValue.Value = false
Camera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end)
I see your code has the usage of UserInputService, so you can use the same service to detect inputs.
-- inside tool.Equipped, outside tool.Activated
local UIS = game:GetService("UserInputService")
UIS.InputBegan.Connect(function(input)
if input.UserInputType == Enum.UserInputType.Touch then
tool:Activate() -- or a function to fire the bullet
if input.KeyCode == Enum.KeyCode.ButtonX then
tool:Activate()
end)
-- rest of the code
Ok, so now its working for xbox, what about tablet and phone? Also for computer players, when you right click the mouse to turn around in-game, it won’t work. It also shoots a whole bunch of bullets per click, which should only shoot 1 bullet per click. Thats about all the bugs right now. You don’t need to fix the bullet one if you want.
For mobile devices it should work, It’ll show a button where they can shoot and zoom in.
This should fix both of your issues.
local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local maxAmmo = 14
local CoolDown = 0.23
local ammo = maxAmmo
local reloading = false
local Camera = game:GetService("Workspace").CurrentCamera
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
local CAS = game:GetService("ContextActionService")
local plrgui = client.PlayerGui
local text = plrgui:WaitForChild("Ammo").AmmoCount
local BoolValue = script.Parent:WaitForChild("EquippedValue")
local ZoomTypes = { Enum.UserInputType.MouseButton2, Enum.KeyCode.ButtonL2 }
local ReloadTypes = { Enum.KeyCode.R, Enum.KeyCode.ButtonX }
local FireTypes = { Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2 }
local gunCooldown = false
local function GunHandler(ActionName, inputState, inputObject)
if ActionName == "WeaponZoom" and BoolValue.Value and inputState == Enum.UserInputState.Begin and (table.find(ZoomTypes, inputObject.UserInputType) or table.find(ZoomTypes, inputObject.KeyCode)) then
Camera.FieldOfView = 40
client.Character:WaitForChild("Humanoid").WalkSpeed = 14.5
plrgui:WaitForChild("HotBarGUI").Enabled = false
end
if ActionName == "WeaponZoom" and inputState == Enum.UserInputState.End and (table.find(ZoomTypes, inputObject.UserInputType) or table.find(ZoomTypes, inputObject.KeyCode)) then
Camera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end
if ActionName == "Reload" and BoolValue.Value and table.find(ReloadTypes, inputObject.KeyCode) and not reloading then
reloading = true
tool.Handle.reload:Play()
task.wait(1)
ammo = maxAmmo
reloading = false
end
if ActionName == "FireWeapon" and BoolValue.Value and (table.find(FireTypes, inputObject.UserInputType) or table.find(FireTypes, inputObject.KeyCode)) and not reloading and ammo > 0 and not gunCooldown then
gunCooldown = true
ammo = ammo - 1
task.wait(CoolDown)
remote:FireServer(cursor.Hit.Position)
text.Text = (ammo).." / "..tostring(maxAmmo)
tool.Handle.gunshot:Play()
tool.Handle.PointLight.Enabled = true
task.wait(0.2)
tool.Handle.PointLight.Enabled = false
task.delay(CoolDown, function() gunCooldown = false end)
elseif ActionName == "FireWeapon" and BoolValue.Value and (table.find(FireTypes, inputObject.UserInputType) or table.find(FireTypes, inputObject.KeyCode)) and not reloading and not gunCooldown then
reloading = true
tool.Handle.reload:Play()
task.wait(1)
ammo = maxAmmo
reloading = false
tool.Handle.gunshot:Stop()
end
end
task.spawn(function()
while true do
text.Text = (ammo).." / "..tostring(maxAmmo)
task.wait()
end
end)
tool.Equipped:Connect(function()
BoolValue.Value = true
cursor.Icon = "rbxassetid://42445293"
plrgui.Ammo.Enabled = true
text.DisableScript.Disabled = true
plrgui.Shop.Enabled = false
plrgui.Inventory.Enabled = false
CAS:BindAction("FireWeapon", GunHandler, true, Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2)
CAS:BindAction("Reload", GunHandler, true, Enum.KeyCode.R, Enum.KeyCode.ButtonX)
CAS:BindAction("WeaponZoom", GunHandler, true, Enum.UserInputType.MouseButton2, Enum.KeyCode.ButtonL2)
end)
script.Parent.Unequipped:Connect(function()
plrgui.Ammo.Enabled = false
text.DisableScript.Disabled = false
plrgui.Shop.Enabled = true
plrgui.HotBarGUI.Enabled = true
plrgui.Inventory.Enabled = true
BoolValue.Value = false
Camera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
CAS:UnbindAction("FireWeapon")
CAS:UnbindAction("Reload")
CAS:UnbindAction("WeaponZoom")
end)