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?
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.
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)
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
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?
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
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