Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a part move on touch?

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

2 comments

0

You are not connecting onTouch to an event, nor did you use enough ends.

0

You’re not calling the function and there isn’t enough ends for your conditional statements

2 answers

Hello. The function onTouch never 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)

1 comment

0

If your gonna indent, INDENT WITH DA TAB KEY

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

1 comment

0

Yours and Thetacah are not usefuls scripts thanks. It just doesn't work and it won't work! Please answer me!

This discussion is preserved as it was. New answers, comments, and votes are closed.