Only one key plays

Hello there,

I’ve been having this problem with my shield where if you press V, it welds to your arm, then when you press X and Z they do different raising positions. Only one of the keys (the one at the beginning) is working. Does anyone know how to get them to all work when it get’s pressed. Any help is appreciated.

Code in Shield
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local equipped = false
local up = false
local baseShield = script.Parent.Parent
local shield  
local UserInputService = game:GetService('UserInputService')

local equip_event = game.ReplicatedStorage.Events:WaitForChild("Equip_Event")
local raise_event = game.ReplicatedStorage.Events:WaitForChild("Raise_Event")
local shield_remove = game.ReplicatedStorage.Events:WaitForChild("Remove_Shield")


player:GetMouse().KeyDown:connect(function(k)
    if not character:findFirstChild("Left Arm") then return end
    if not character:findFirstChild("Torso") then return end
    if not character.Torso:findFirstChild("Left Shoulder") then return end
   
   





 if k == "v" then
	      if equipped == false then
            equipped = true
        elseif equipped == true then
          equipped = false 
end
       
equip_event:FireServer(baseShield)
      
 
 if k == "x" then
	     
     raise_event:FireServer()

   
 
elseif k == "z" then
	 
	
shield_remove:FireServer()



end    
end
end)
1 Like

Could you please fix the formatting before posting? It becomes much easier to see the problem when you do

player:GetMouse().KeyDown:connect(function(k)
	
	if not character:findFirstChild("Left Arm") then return end
	if not character:findFirstChild("Torso") then return end
	if not character.Torso:findFirstChild("Left Shoulder") then return end

	if k == "v" then
		if equipped == false then
			equipped = true
		elseif equipped == true then
			equipped = false 
		end
		equip_event:FireServer(baseShield)
		if k == "x" then
			raise_event:FireServer()
		elseif k == "z" then
			shield_remove:FireServer()
		end
	end
end

You check if the v key is pressed, and then under that same if-block you check the other keys, you have to have a different condition. Here is a fixed version.

	if k == "v" then
		equipped = not equipped
		equip_event:FireServer(baseShield)
	elseif equipped then
		if k == "x" then
			raise_event:FireServer()
		elseif k == "z" then
			shield_remove:FireServer()
		end
	end
1 Like

I tried your solution but the X and Z keys still don’t work. Any other solution?

print(key, equipped) -- Before checking the v key
1 Like

The print shows, and the prints in the server script show as well, but the raise c-frame doesn’t play. Is there something I need to change in the server as well?

Server Script
game.ReplicatedStorage.Events:WaitForChild("Raise_Event").OnServerEvent:Connect(function(player, baseShield)
local character = player.Character or player.CharacterAdded:wait()
local equipped = true
local up = false   print("Step4")
local shield
local lsh = character.Torso:findFirstChild("Left Shoulder")
local UserInputService = game:GetService('UserInputService')
        if not up then
           local w = Instance.new("Weld",shield)
            w.Part0 = character.Torso
            w.Part1 = character["Left Arm"]
            w.C1 = CFrame.new(1.2,-.25,0.05) * CFrame.fromEulerAnglesXYZ(math.rad(-75),math.rad(55),0)
       print("Step6")

        end
end)

Shield is nil, you meant baseShield.

 local w = Instance.new("Weld", baseShield)
1 Like

Use the ContextActionService method.

3 Likes

Yeah, the Mouse input events are actually deprecated. For all new work, you should make sure to use one of those two services quoted.


As a beginner scripter, one if the things you need to remember about programming is that you need to tell it exactly what to do. It won’t automatically understand what your intent is – so for cases like this you should be really breaking your code into logical steps so you don’t skip any important bits and so you can find any weird logic (like k equalling both ‘v’ and ‘x’/‘z’ at the same time, which is impossible). It seems a bit confusing at the start, but eventually thinking like this becomes second nature :slight_smile:

1 Like