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

No, I’m using the arrow keys on my keyboard to move the part (which for me just does not work).

Ok so here is the problem. Activated is an event for when you click on your mouse while the tool is out. So your db varaible will not change if you don’t click when the tool is out.

tool.Activated:Connect(function() -- detects when player uses the tool (i.e clicks their mouse)
	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)

If you want your script to run when you have the tool out, you need to use the Equipped event instead of Activated

Does that mean that to move the part, you would need to click the mouse and the arrow key at the same time, or would you just need to click the mouse once and then it’s ok?

No let me explain this real quick. :>

So your db variable is already set to false meaning you can’t move the part. In order to change the db variable, your code wants you to activate the tool (i.e take it out and click on your mouse)

tool.Activated:Connect(function() -- detects when player uses the tool (i.e clicks their mouse)
	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)

From there your variable is now changed to true and your UIS function will now work because the conditions are met.

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)

Your code also deactivates the UIS function when you click a second time.

Ah it makes a bit of sense to me now. Thanks for helping! Is there anything else I should know or maybe change in the script?

Glad I could help! Your script seems fine, to make it more efficient you could use the Deactivated event as well, but it’s no huge deal either way the code works. If there is any line of code that’s confusing though I’d be happy to explain! :>

Thanks once again, and would it be a problem for you to reply to you again if I had a question or something like that?

Maybe be better to message me rather than reply to this post, since the problem is solved?? But no I don’t think it would if you’d rather just make another reply in case others had questions.

That’s true, I’ll message you if I do have any questions. Anyways, have a good day and thanks for wasting your time to help me lol

You’re welcome! And no worries it was not a waste of my time, I enjoy helping out peeps :>

1 Like

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