Need help with a localscript for moving a part using keyboard

Hi! I need help with this localscript which is supposed to move a part when the arrow keys are pressed. But I only want it to be movable when the tool (which moves the part) is equipped.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local movingpart = workspace.Union

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	print(gameProcessedEvent) -- check to see if game recognizes input
	if not gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.Left then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, -1)
		print("Moving part left")
	elseif input.KeyCode == Enum.KeyCode.Right then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, 1) 
		print("Moving part right")
	end
end)

For some reason, I’m unable to move the part when the tool is equipped. However when it’s unequipped, then I’m able to move the part. How can I fix this? Also, sometimes I can’t move the part at all and nothing comes up in the output. Can somebody help me out?

gub location of where you put the local script

I kept the localscript inside of a tool. The tool is placed in the StarterPack.

I see, well you know that when tools are equipped, they go into the player character model inside the workspace

Local scripts don’t work when it is inside the workspace

So inside the local script, you can move it somewhere else like:

-- inside your local script
local tool = script.Parent -- still parented to the tool

script.Parent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerScripts")

You can use the variable tool if you want to access stuffs inside the tool

It seems to be working better now. But, could you help me to make it so that you can only move the part when the tool is equipped. And then when it’s unequipped, you can’t move the part anymore.

do something like this:

local inputConnection

function inputBeganFunc(input, gameProcessedEvent)
 -- your code for "InputBegan" here
end)

tool.Equipped:Connect(function()
 inputConnection = uis.InputBegan:Connect(inputBeganFunc)
end)

tool.Unequipped:Connect(function()
 inputConnection:Disconnect() -- disconnect event when unequipped
end)

Could you explain the script to me? I’m not a scripter so I don’t understand much (also I just used a tutorial to write the script mainly)

Sooo…

Equipped and Unequipped are events that fire if the tool is equipped or unequipped respectively.

uis.InputBegan is an event that fires when you enter an input from your keyboard and from other input devices like a controller or etc.

All events take in a function like uis.InputBegan:Connect(poop) in this case, your function is called “poop”

You can either do:

function poop(input, gameProcessedEvent)
 print("Hello poop") -- code here
end

uis.InputBegan:Connect(poop)

or

uis.InputBegan:Connect(function(input, gameProcessedEvent)
 print("Hello poop") -- code here
end)

The code I wrote will print “Hello poop” into the output console.
Both ways above will do the same thing.

Since your code for the when InputBegan fires is:

    print(gameProcessedEvent) -- check to see if game recognizes input
	if not gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.Left then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, -1)
		print("Moving part left")
	elseif input.KeyCode == Enum.KeyCode.Right then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, 1) 
		print("Moving part right")
	end
1 Like

You can put inside the “inputBeganFunc” function like this:

local inputConnection

function inputBeganFunc(input, gameProcessedEvent)
    print(gameProcessedEvent) -- check to see if game recognizes input
	if not gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.Left then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, -1)
		print("Moving part left")
	elseif input.KeyCode == Enum.KeyCode.Right then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, 1) 
		print("Moving part right")
	end
end)

tool.Equipped:Connect(function()
 inputConnection = uis.InputBegan:Connect(inputBeganFunc)
end)

tool.Unequipped:Connect(function()
 inputConnection:Disconnect() -- disconnect event when unequipped
end)

Sorry for the late response, I needed to go somewhere. Thank you for the script! Is there any way where I can make it so that once you unequip it, you can’t move the part at all. Right now, I can’t move the part if I don’t have it equipped. But then when I do equip it, I can move it. Once I unequip it after that, I can still move the part. Is there any way to prevent the last part?

I’ll look into it and try to make my own

I found another way:

local uis = game:GetService("UserInputService")

local isEquipTool = false

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if not isEquipTool then return end
	if not gameProcessedEvent then return end
	
	if input.KeyCode == Enum.KeyCode.Left then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, -1)
		print("Moving part left")
	elseif input.KeyCode == Enum.KeyCode.Right then
		movingpart.Position = movingpart.Position + Vector3.new(0, 0, 1) 
		print("Moving part right")
	end
end)

local tool = script.Parent

tool.Equipped:Connect(function()
	isEquipTool = true
end)

tool.Unequipped:Connect(function()
	isEquipTool = false
end)

This way, instead of disconnecting the event, we have a variable isEquipTool which store a boolean (true or false) and we use it to check if the user is equipping it or not.

And also found out that local script still work in the Workspace as long as it is inside your player character model, so no need to change the local script’s parent

1 Like

What would my entire script to move the part using arrow keys be, if I included this?

Just wanted to confirm how to write the entire script, but I tested it with what you added and it works! Thank you so much and have a good day! :slight_smile:

No problem! You can print out the input.KeyCode then when you press the key that you want to use and it will print out the word for it. Example: If I press the G key on my keyboard, it will print out Enum.KeyCode.G

1 Like

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