Problem with player controls

Hello, So I need help with a script.
What I am trying to do is that when the player presses “D” a block moves 1 stud to the right and if they press “A” a block goes 1 stud to the left but if the block touches a block called “End” it wont move in the direction the player wants to go.

Here is my script.

local test = game.Workspace.TestPlayer
local UserInputService = game:GetService(“UserInputService”)

UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.D then

end

end)

2 Likes

Probably something like this:

local testplayer = game.Workspace.TestPlayer
local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.D then
			testplayer.Position = testplayer.Position + "YOUR_INCREMENT" -- change this to a number
		elseif input.KeyCode == Enum.KeyCode.A then
			testplayer.Position = testplayer.Position - "YOUR_INCREMENT" -- same thing
		end
	end
end)

testplayer.Touched:Connect(function(hit)
	if hit.Parent.Name == "End" then
		testplayer.Position = testplayer.Position + "YOUR_INCREMENT"
	end
end)

I suppose this is the code, note that i wrote this for the block you supposedly want to move.

1 Like

There’s Probably a better way to do this code, I tried my best :confused:

I just tried it and I’m getting this error.

Workspace.Funny_Develop.PlayerControls:7: attempt to perform arithmetic (add) on Vector3 and string

I assumed that you kept the "YOUR INCREMENT" instead of changing it to a number

local testplayer = game.Workspace.TestPlayer
local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.D then
			testplayer.Position = testplayer.Position + Vector3.new(1, 0, 0) -- change this to a number
		elseif input.KeyCode == Enum.KeyCode.A then
			testplayer.Position = testplayer.Position - Vector3.new(1, 0, 0) -- same thing
		end
	end
end)

testplayer.Touched:Connect(function(hit)
	if hit.Parent.Name == "End" then
		testplayer.Position = testplayer.Position + Vector3.new(increment, 0, 0)
	end
end)

also don’t chnage them to a number they should move one stud right and left

I changed it to “1”

I just tried changing it to a higher number but it still didn’t work

a "1" would be still considered as a string since you wrapped it around quotation marks

1 Like

he also needs to make it a vector3 value so it knows what axis to change

1 Like

Do you know how to apply this to GUI’s?

I don’t understand please explain more