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