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