Script Help and Feedback

Hey I want to make a part in my game that when you touch it, it moves 10 studs or so studs but I can’t figure it out. It may be more complicated than I first thought but I would still like to make it. Here is the code that I wrote, local Variable = (1)

game.Workspace.Egg.Touched:Connect(function()

end)

while true do

script.Parent.Position = Vector3.new(10 , 10 , 10)

end

This is probably very wrong but I tried.

Your original code splits the moving & touching into sections, instead you need to combine them.

You should instead try this:

-- You should know what this does ;)
game.Workspace.Egg.Touched:Connect(function(obj)
     -- We need this to check if it's a player, not a random part or something
     local player = game.Players:GetPlayerFromCharacter(obj.Parent)

     if player then
          -- Move it forward 10 studs on the X AXIS
          game.Workspace.Egg.Position = game.Workspace.Egg.Position + Vector3.new(10, 0, 0)
     end 
end)

I wrote this quickly, so there might be some small mistakes/errors, so reply & let me know if it doesn’t work out.

Try making the Vector3.new(10, 0, 0) now Vector3.new(0, 10, 0) or fill in those numbers with anything and see what happens! The key to learning is experimenting.

1 Like

Thanks! I used it and now it works! My only question is what’s obj? and I tried (0, 10, 0) and it moves slightly but drops on you from up high!

3 Likes

When you do a touched event, obj (you can name it anything, I just use obj) is the thing that touched it.

X, Y, Z, because you are changing to Y, you are changing the height. The reason it keeps falling is because it is un-anchored, open the properties window, search “anchor” while selecting the part, and set it to true! This makes it so it doesn’t fall.

1 Like