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

it wont teleport for some reason. I'm using math.random and im not that good at it?

game.Players.PlayerAdded:Connect(function(plr)
	while true do
		wait(1)
	local character = plr.Character
		character.HumanoidRootPart.Position = CFrame.new(math.random(118.701, 2.9, 61.094), math.random(60.81, 2.9, 94.995))
	end
end)

1 comment

0

Hey I see you're having trouble, ill explain it for you. :D

2 answers

✓ Accepted answer

Math.random() requires two values: min, and max. It returns an integer from min to max, unless Math.random() is not given any arguments. When not given an argument, it returns a value between 0 and 1. This means that it can return something bizare like 0.512333333333.

However, if you want to get an irrational number from math.random(), you can multiply the base function by a constant (like math.random()*100), or divide math.random() with arguments by a constant (like math.random(1, 5000)/100).

If you want to give a random position, you should use math.random three times for the CFrame, like this:

character.HumanoidRootPart.CFrame = CFrame.new(math.random(0, 118), 2.9, math.random(60, 94)) or something along those lines depending on what you wish. Enjoy!

p.s. position weld/constraint-bound objects via CFrame otherwise their welds would be deleted and they'll fall out of the world. :(
p.p.s position should be cframe. cframe is like cursive position, it models 3D space and rotation. it also maintains the welds and constraints when used.

1 comment

0

If you want to maintain the player's rotation, you should call CFrame:ToEulerAnglesXYZ(), like so: HumanoidRootPart.CFrame = CFrame.new() * CFrame.Angles(HumanoidRootPart.CFrame:toEulerAnglesXYZ())

So the first error that I see, you're using CFrame, and that you should use Vector3 like this:

character.HumanoidRootPart.Position = Vector3.new()

Also, Vector 3 should have 3 parameters, for example:

character.HumanoidRootPart.Position = Vector3.new(0, 0, 0) -- 0,0,0 are parameters

But you're trying to make it go random right? So thats when math.random come in! Make sure your math.random has 2 parameters and not 3. Something like:

character.HumanoidRootPart.Position = Vector3.new(math.random(1, 60), math.random(1, 60), math.random(1, 60))

You can edit this anyway possible. And thats all! Hope this works and helps you out! :D

5 comments

0

You should NEVER use Vector3 on character descendants. They will break any joints related and will end up falling through the world. Always position Humanoid parts via CFrame.

0

Actaully, if it's bound by a weld or constraint (Motor6Ds included), then just use CFrame. When in doubt, you should probably use CFrame.

0

Just because it doesn't error doesn't necessarily mean it will work the way you intend it to.

0

But he was calling Position, and not CFrame, thats why I used Vector.

0

..I never even noticed that. Oops

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