Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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!

0
wait, what is current position for? that would not work because it is not a variable? rieleyhunt 26 — 5y
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 DaBrainlessOne 129 — 5y
Ad
Log in to vote
0
Answered by
Fad99 286 Moderation Voter
5 years ago

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.

0
Also make sure the part isint anchored, so it can fall to the ground Fad99 286 — 5y
0
CFrame is not a valid member of Egg. Um rieleyhunt 26 — 5y
0
hmm Fad99 286 — 5y
0
Can you post your code somewhere so i can see it this worked for me Fad99 286 — 5y

Answer this question