Hi, I would like to know if there is a way to get the current position of a part and use it to change the position relative to where it is. I am trying to make a Minecraft Dragon Egg and it needs to subtract a value from where it is in the present.
--minecraft dragon egg
bounce = true
function teleportEgg()
if bounce == true then
bounce = false
x = math.random(1, 20)
y = math.random(1, 20)
z = math.random(1, 20)
script.Parent.Position = Vector3.new(currentposition - x, currentposition - y, currentposition - z)
end
end
script.Parent.ClickDetector.Mouseclick:Connect(teleportEgg)
Before I start the actual answer I'd just like you to know you can add and subtract to a position at the same time so that the egg isn't forced to just go down; it can also go up.
You could do something like
local x = math.random(-10,10)
Anyways for the answer, you would just do
local currentposition = script.Parent.Position
script.Parent.Position = Vector3.new(currentposition.X - x, currentposition.Y - y, currentposition.Z - z)
Just add a .X, .Y, .Z to the end of the respective coordinate value. Good luck with your game!
p.s. please use code blocks in future questions!
What you are looking for is CFrame:ToWorldSpace()
. It can move or rotate an object relative to a part, and you can move it relative to itself too.
local Egg = script.Parent local Click = Egg.ClickDetector Click.MouseClick:Connect(function(Player) local Rand = math.random(-20,20) local Rand2 = math.random(0,5) local Rand3 = math.random(-20,20) Egg.CFrame = Egg.CFrame:ToWorldSpace(CFrame.new(Rand,Rand2,Rand3)) end)
What this script does is move the egg to a random location left, right, and a little up from the current position. I recommend adding a body gyro into the egg so it dosent roll over.