Keybind toggle script

I am wanting to make it so that if you press a key, this will then toggle a serverscript which will move the players arm.

I am not sure what I’ve done wrong, however, nothing I try seems to work. I would just like to know what I’ve done wrong and how I can fix it.

Here is my current script

local Player = script.Parent.Parent
local Char = Player.Character
local UIS = game:GetService("UserInputService")

local debounce = false
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.E then
		if not debounce then
			debounce = true
			local Weld = Instance.new("Weld")
			Weld.Name = "Tool_Weld1"
			Weld.Part0 = Char.Torso
			Weld.Part1 = Char['Right Arm']
			Weld.Parent = Char.Torso
			Weld.C1 = CFrame.new(-1.5, 0.75, -0.5) * CFrame.fromEulerAnglesXYZ(math.rad(180), 0, math.rad(-20))
		else
			Char.Torso:FindFirstChild("Tool_Weld1"):Destroy()

			debounce = false
		end
	end  
end)

Thank you!

Is this a LocalScript or a ServerScript, and what do you mean by activating a serverscript, I see nothing of the sort

You are doing all of this in a local script. What you need to do is have the local script fire a RemoteEvent when the E key is pressed. On the server (in some server script), listen for when the RemoteEvent is fired from the client and then move the arm on the server.

You will need 2 scripts for this, one local and one server script. Here is the documentation for RemoteEvents.

Hope this helps :slight_smile:

1 Like

Scripts won’t work in StarterPlayerScripts, only LocalScripts.

Try to use this:
Create 2 RemoteEvent’s in ReplicatedStorage (Name first one “WeldCreator”, and the second one “WeldRemover”), And then Create LocalScript in StarterPlayerScripts with script:

local Player = script.Parent.Parent
local Char = Player.Character
local UIS = game:GetService("UserInputService")

local debounce = false
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.E then
		if not debounce then
			debounce = true
			game:GetService("ReplicatedStorage").WeldCreator:FireServer()
		else
			game:GetService("ReplicatedStorage").WeldRemover:FireServer()
			debounce = false
		end
	end  
end)

And create Script in “ServerScriptService” with this script:

game:GetService("ReplicatedStorage").WeldCreator.OnServerEvent:Connect(function(player)
local Weld = Instance.new("Weld")
Weld.Name = "Tool_Weld1"
Weld.Part0 = player.Character.Torso
Weld.Part1 = player.Character['Right Arm']
Weld.Parent = player.Character.Torso
Weld.C1 = CFrame.new(-1.5, 0.75, -0.5) * CFrame.fromEulerAnglesXYZ(math.rad(180), 0, math.rad(-20))
end)

game:GetService("ReplicatedStorage").WeldRemover.OnServerEvent:Connect(function(player)
player.Character.Torso:FindFirstChild("Tool_Weld1"):Destroy()
end)

The result i got:
image
My character is saluting

1 Like