Help needed with scripting the movement of a part using arrow keys

I really need help with this script. I’m not a scripter in any way so please don’t destroy me in the replies lol (also I just used a tutorial). So basically, I have a part that I want to be able to move using the arrow keys while have a tool equipped. This is the localscript I have written using a tutorial, however it doesn’t work and nothing below comes up in the output. Please help.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local db = false

local uis = game:GetService("UserInputService")
local movingpart = workspace.Union

script.Parent.Activated:Connect(function()
	if db == false and hum then
		db = true
		hum.WalkSpeed = 0
		print("Script activated")
	else
		db = false
		hum.WalkSpeed = 16   
		print("Script deactivated")
	end
end)

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

From experience alone I can attest to the fact that player.CharacterAdded:Wait() does not work on a tool. The reason why it prints nothing is because when it gets to

local hum = char:WaitForChild("Humanoid")

it has no reference for the “character” because the character does not exist, or so I would suspect. That or it’s stuck in a loop elsewhere. Instead of what you have for the character, replace local char = player.Character or player.CharacterAdded:Wait() with

local char

repeat char = player.Character wait(1) until char ~= nil

This should resolve the problem.

1 Like

I replaced the local char part but it still doesn’t work. Also, I just want to let you know that I have another script which is linked to the same part (it makes the part rotate infinitely). Could that have something to do with this?

1 Like

Has to do with your gameProcessedEvent if statement. When you press the arrow keys it returns true meaning the game recognized the input and since it did your if statement tells the function to stop.

Add not before gameProcessedEvent then return end and it should hopefully fix your problem.

2 Likes

I made the change but unfortunately it still doesn’t work. I’m guessing it’s something to do with the local hum and local char stuff

1 Like

Do you think there could be any other faults in the code? (100% are but idk where at all)

1 Like

No your humanoid and character should be defined perfectly fine. My other guess would be that it has something to do with your debounce variable. Try printing db above your KeyCode if statements to see if something is off.

1 Like

Nothing comes up in the output when I print db above the keycode if statements.

1 Like

Did you fix your gameProcessedEvent if statement? If not print that variable above that if statement to see what’s going on or temporarily remove it.

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

Same issue, nothing is coming up in the output. I’m starting to think something is broken…

1 Like

My guess is it most likely has to do something with your script being even enabled then or where it is placed.

script.Parent.Activated:Connect(function()
	if db == false and hum then
		db = true
		hum.WalkSpeed = 0
		print("Script activated")
	else
		db = false
		hum.WalkSpeed = 16   
		print("Script deactivated")
	end
end)

First: is the script activated?
Second: what is the ancestor/parent (is it in playerscripts, startergui, etc.)?

1 Like

Idk if the script is activated, could you give me a way to find out if possible?

The script is a localscript placed inside of a tool which is in StarterPack

1 Like

when you test your game, open explorer → find the script → open script’s properties
image

1 Like

I have just checked and it is indeed enabled.

1 Like

Interesting… I just replicated your script and it seems to work fine on my end. Can I see your updated script by any chance?

1 Like

Ok now I’m nervous because I’m scared it was a fault on my end lol

Here’s my updated code:

local player = game.Players.LocalPlayer
local char
repeat char = player.Character wait(1) until char ~= nil
local hum = char:WaitForChild("Humanoid") wait(1) until char ~= nil
local db = false

local uis = game:GetService("UserInputService")
local movingpart = workspace.Union

script.Parent.Activated:Connect(function()
	if db == false and hum then
		db = true
		hum.WalkSpeed = 0
		print("Script activated")
	else
		db = false
		hum.WalkSpeed = 16   
		print("Script deactivated")
	end
end)

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

No worries! This happens often with programming, nothing to feel bad about.

I would revert this section of code:

local char
repeat char = player.Character wait(1) until char ~= nil
local hum = char:WaitForChild("Humanoid") wait(1) until char ~= nil

back to this, because it worked fine before, so there was no need to change it:

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

I also forgot to incorporate your db variable in my script, lol. Everything still works, but your db variable is inflicting. What is its purpose exactly?

try this:

local player = game.Players.LocalPlayer
local char
local tool = script.Parent

repeat 
	wait(1)
	char = player.Character 
until char

local hum = char:WaitForChild("Humanoid")
local db = false


local uis = game:GetService("UserInputService")
local movingpart = workspace.Union

tool.Activated:Connect(function()
	if db == false and hum then
		db = true
		hum.WalkSpeed = 0
		print("Script activated")
	elseif hum then
		db = false
		hum.WalkSpeed = 16   
		print("Script deactivated")
	end
end)

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then return end -- suposed to be like that because arrow left and right keys are events handled by roblox.
	
	if input.KeyCode == Enum.KeyCode.Left and db == true then
		movingpart.Position = movingpart.Position + Vector3.new(-3, 0, 0)
		print("Moving part to the left")
	elseif input.KeyCode == Enum.KeyCode.Right and db == true then
		movingpart.Position = movingpart.Position + Vector3.new(3, 0, 0)
		print("Moving part to the right")
	end
end)

Edit:
There is gameProcessedEvent in uis.inputBegan and gameProcessedEvent is equal to true when roblox is handling it’s events so…
Arrow keys are roblox handled events that’s why it won’t work and your code didn’t work too before

Haha thanks! Well I just used a tutorial so I don’t understand the script 100% but I’m pretty sure it was used to prevent spamming.

Are you clicking your mouse when you have the tool out? I just realized its purpose lol. The script is working fine.