Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a part move on touch?

Asked by 6 years ago
Edited 6 years ago

I am trying to make a part move when a player touch it. I have tried alot of scripts and none of them worked. Here is the last one i tried:

brick = game.Workspace.partypartpart

function onTouch(hit)
    if hit.Parent.Humanoid then
 script.Parent.Position = script.Parent.Position + Vector3.new(0, 0, 10)
 wait (1)
 script.Parent.Position = script.Parent.Position + Vector3.new(0, 0, 0)
 wait (1)
    end

Any help please? Thanks!

~Del

0
You are not connecting onTouch to an event, nor did you use enough ends. hiimgoodpack 2009 — 6y
0
You’re not calling the function and there isn’t enough ends for your conditional statements Subaqueously 11 — 6y

2 answers

Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
6 years ago

Hello. The function onTouchnever fires since you never added a :connect() line which tells the function to run upon the .Touched event.

Its format would be,

partName.EventName:Connect(functionName); --So for your case, it would look like: 

brick.Touched:Connect(onTouch)

Both if statements and function require an end line. It appears as if you've only got one, so you need another end.

Your functional code should look like this:



function onTouch(hit) if(hit.Parent.Humanoid) then script.Parent.Position = script.Parent.Position + Vector3.new(0, 0, 10) wait (1) script.Parent.Position = script.Parent.Position + Vector3.new(0, 0, 0) wait (1) end end brick.Touched:Connect(onTouch)
0
If your gonna indent, INDENT WITH DA TAB KEY hiimgoodpack 2009 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

There’s an easier way to do functions instead of writing them and calling them, however in some cases this method is not preferred over calling it separately. But, for now this is a faster method for calling a function once the Touched event occurs.

On Line 1, we are defining the variable so it is easier than typing it out later in the script. We use local variables so it is only accessible in the function and nowhere else in the script. On Line 3 we are saying if the brick is touched, then this happens. On the next line, however, we say that this only happens if something with a humanoid in it touches it (Player). Then, we tell it to move 10 studs in the z direction. We wait one second, and then end the function.

local brick = workspace.partypartpart

brick.Touched:Connect(function(hit))
      it hit.Parent:FindFirstChild(“Humanoid”) then
            brick.CFrame = brick.CFrame + Vector3.new(0, 0, 10)
            wait(1)
      end
end
0
Yours and Thetacah are not usefuls scripts thanks. It just doesn't work and it won't work! Please answer me! roblox8881215 17 — 2y

Answer this question