How can i make a script that when i press the key "E" a part named "NicePart" change X by 1
Try something like this
local part = script.Parent
part.Position = part.Position * Vector3.new(5, 0, 0)
This changes the parts position on the X axis by 5. You can also use tweenService to move a part.
I have written this script with no testing, if there are any errors reply to this post.
First of all, to detect User-Input you need to do it in the client, but if you change the position of the part in the client it won't show to other people, thus can be only done with "remote events".
Make a remote event called 'Input' in the ReplicatedStorage
Make a local script in the starter pack(doesn't matter where ever you do it) called InputReceiver.
Make a script in the 'ServerScriptService' called EventCaller.
In the local script:
local ReplicatedStorage = game:GetService('ReplicatedStorage') -- defining replicated storage to use waitforchild local Event = ReplicatedStorage:WaitForChild('Input') -- the event we are going to fire local UIS = game:GetService('UserInputService') -- the service we use to detect the key the player is pressing UIS.InputBegan:Connect(function(input,isTyping) -- runs when ever the player presses the key you want if isTyping then -- checking if the player isn't typing return elseif input.KeyCode == Enum.KeyCode.E then -- if the input is == to our key Event:FireServer() -- tells the server to move the part end end)
Now to make things clear: You said only changing the x value, which means I have to explain it first
part.Position = part.Position * Vector3.new(1,0,0)
By leaving the rest axis 0 we make it worthless thus allowing us to only move the axis that we entered. For more information check the dev forum.
In the server script:
local part = workspace:WaitForChild('Part') local ReplicatedStorage = game:GetService('ReplicatedStorage') local Event = ReplicatedStorage:WaitForChild('Input') -- receiving the event Event.OnServerEvent:Connect(function() part.Position = part.Position * Vector3.new(1,0,0) end)
I have written this script with no testing, if there are any errors reply to this post.