hello, i’d like to make an object move using the player’s directional buttons. for example, if the player clicks “W” and goes forward, so does the object. same thing with the other buttons and jump. unfortunately i am very bad at scripting and i though some of you might help me with the code, thank you!
You could use UserInputService and then move the cframe of the object when the key is pressed in the direction you would like. This should help.
sorry but i really dont know where to put my hands
Put this in a localscript and place it in startercharacterscripts (On further inspection this won’t work since if the player turns around, the part will go the wrong way. You could use this as a reference or rework it. Sorry)
local UIS = game.UserInputService
local part = game.Workspace.Part --Change .Part to whatever your part is called.
local speed = 0.5 --Change this to whatever you like
function Move(input)
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.Up then --Check which key was pressed
while UIS:IsKeyDown(Enum.KeyCode.W or Enum.KeyCode.Up) do --Run until key stops being held
wait(0) --Prevents a stack overflow
part.CFrame += Vector3.new(0,0,-speed) --Change the position based on the speed
end
end
if input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.Down then
while UIS:IsKeyDown(Enum.KeyCode.S or Enum.KeyCode.Down) do
wait(0)
part.CFrame += Vector3.new(0,0,speed)
end
end
if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left then
while UIS:IsKeyDown(Enum.KeyCode.A or Enum.KeyCode.Left) do
wait(0)
part.CFrame += Vector3.new(-speed,0,0)
end
end
if input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right then
while UIS:IsKeyDown(Enum.KeyCode.D or Enum.KeyCode.Right) do
wait(0)
part.CFrame += Vector3.new(speed,0,0)
end
end
end
UIS.InputBegan:Connect(Move)
thank you it works, but when the player clicks two buttons at the same time (to go diagonally) the part starts going faster
If you look it up i think you can find a solution. Unfortunately there is a bigger problem this script doesn’t account for, read my edits for context
Ok so to make this work on any device what you can do is use the moveFunction in the player controls. To do this is quite simple.
local thisPlayer = playerS.LocalPlayer --get the player
local playerScripts = thisPlayer:WaitForChild("PlayerScripts") --wait for the player scripts
local playerModule = require(playerScripts:WaitForChild("PlayerModule")) -- wait for the player module
local movementController = playerModule:GetControls() -- get the movement controller
movementController.moveFunction = function(player, direction, relative) -- movement controller for player
thisPlayer.Move(player, direction, relative) -- will move the player
part.Position += direction --replace part with the part you want to move and this will just move the part the same direction as the player
end
That would handle movement. For jumping what you can do is just set the part be the players jump height with this formula: h = (JumpPower ^ 2) / (2 * workspace.Gravity)
local Char = thisPlayer.Character or thisPlayer.CharacterAdded:Wait()
Char.Humanoid.StateEnabledChanged:Connect(function(state,enabled)
if state == Enum.HumanoidStateType.Jumping then
if enabled == true then
part.Position += Vector3.new(0,(Char.Humanoid.JumpPower^ 2) / (2 * workspace.Gravity),0)
elseif enabled == false then
part.Position -= Vector3.new(0,(Char.Humanoid.JumpPower^ 2) / (2 * workspace.Gravity),0)
end
end
end)
This is correct as far as i can tell. Use this solution instead of mine.
at what point in the script should i put this line? also, how do i identify “JumpPower”? and how can i change the speed of the movements?
I fixed this before. Use arithmetic to calculate the direction and speed.
direction = vector3.new()
speed = 1 -- change this to make part slower or faster
directions = {w = 0, a = 0, d = 0, s = 0} -- when a button gets pressed, make it 1
while true do
local z = direction.z
local x = direction.x
x += ((directions.d - directions.a)) * speed) / ((w - s) * (w - s))
z += ((directions.w - directions.s)) * speed) / ((d - a) * (d - a))
direction = vector3.new(x, 0, z)
end
Unfortunately, it does not factor height. However, this fixed script will give you the ability to change the speed and quality of the movements:
direction = CFrame.new(Vector3.new(0,0,0))
speed = 1 -- change this to make part slower or faster
halt = 1 -- change this to make part halt less. Decreasing this will lower performance, while increasing this will make this less realistic
directions = {w = 0, a = 0, d = 0, s = 0} -- when a button gets pressed, make it 1
while true do
local z = direction.z
local x = direction.x
x += ((directions.d - directions.a) * speed) / ((directions.w - directions.s) * (directions.w - directions.s))
z += ((directions.w - directions.s) * speed) / ((directions.d - directions.a) * (directions.d - directions.a))
direction = CFrame.new(Vector3.new(x, 0, z))
wait(halt) --
end
I forgot to take the w, a, s, and d from the directions table and also had a problem with the ()s.
edit: If you want to make the part turn, do this:
direction = CFrame.new(Vector3.new(0,0,0))
speed = 1 -- change this to make part slower or faster
halt = 1 -- change this to make part halt less. Decreasing this will lower performance, while increasing this will make this less realistic
directions = {w = 0, a = 0, d = 0, s = 0} -- when a button gets pressed, make it 1
while true do
local z = direction.z
local x = direction.x
x += ((directions.d - directions.a) * speed) / ((directions.w - directions.s) * (directions.w - directions.s))
z += ((directions.w - directions.s) * speed) / ((directions.d - directions.a) * (directions.d - directions.a))
direction = CFrame.new(Vector3.new(x, 0, z),Vector3.new(x + (directions.d - directions.a,0,z + (directions.w - directions.s)))
wait(halt) --
end
where does the script identify the part tho?
You can change the ‘direction’ variable to ‘part’ and set it to the part then change everything else to part.CFrame instead of direction. Here is an example:
part = -- change this to the part
speed = 1 -- change this to make part slower or faster
halt = 1 -- change this to make part halt less. Decreasing this will lower performance, while increasing this will make this less realistic
directions = {w = 0, a = 0, d = 0, s = 0} -- when a button gets pressed, make it 1
while true do
local z = part.CFrame.z
local x = part.CFrame.x
x += ((directions.d - directions.a) * speed) / ((directions.w - directions.s) * (directions.w - directions.s))
z += ((directions.w - directions.s) * speed) / ((directions.d - directions.a) * (directions.d - directions.a))
part.CFrame = CFrame.new(Vector3.new(x, 0, z),Vector3.new(x + (directions.d - directions.a), 0, z + (directions.w - directions.s)))
wait(halt) --
end
Edited it. Hopefully it will work this time.
I think that happened when I was pasting it. It should be fine once you delete that part. (Editing script)
the part disappears and reappears
Do you mean it looks like it would be super laggy? If so, just decrease the halt and speed.
Question: do you want the player’s character to be a fish and still be able to move with WASD keys?