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

I have a question about Position and storing all 3 arguements of it?

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)

2 answers

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!

2 comments

0

wait, what is current position for? that would not work because it is not a variable?

0

It should work as long as the variable is script.Parent.Position. If it's not, then I changed my answer to include it. Also you should use local variables more often

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.

5 comments

0

Also make sure the part isint anchored, so it can fall to the ground

0

CFrame is not a valid member of Egg. Um

0

hmm

0

Can you post your code somewhere so i can see it this worked for me

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