Shoot Script on Z key not working

This script spawns a projectile but it is shooting on all keys on the keyboard, I only want it to shoot on the Z key.

local Player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
uis.InputBegan:connect(function(input,processed)
	if input.UserInputType == Enum.UserInputType.Keyboard and not processed then
			if Enum.KeyCode.Z then --The shoot key
			local Part = game.ReplicatedStorage.BasicProjectile:Clone()
			Part.Parent = game.Workspace
			Part.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(-.1,3,-5)
			local y = Instance.new("BodyVelocity")
			y.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			y.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector*100
			y.Parent = Part
			Part.Orientation = script.Parent.Turret.Orientation
			script.Disabled = true
			wait(0.2)
			script.Disabled = false
		end
	end
end)
1 Like

You’re not checking if the input is Z, only if Enum.KeyCode.Z is a thing.

local Player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
uis.InputBegan:connect(function(input,processed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Z  and not processed then
			local Part = game.ReplicatedStorage.BasicProjectile:Clone()
			Part.Parent = game.Workspace
			Part.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(-.1,3,-5)
			local y = Instance.new("BodyVelocity")
			y.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			y.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector*100
			y.Parent = Part
			Part.Orientation = script.Parent.Turret.Orientation
			script.Disabled = true
			wait(0.2)
			script.Disabled = false
	end
end)

Try this

2 Likes

Thank you, It works very good :smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.