The title says it, as how i would teleport to behind or front or right or left of the Player?
Black = example of random positions of where to teleport in a circular form
Blue = the player
The title says it, as how i would teleport to behind or front or right or left of the Player?
Black = example of random positions of where to teleport in a circular form
Blue = the player
Just to confirm; You want an imaginary circle to form around the player, and on a random point of that circle, something is teleported?
Yeah i guess, it is Imaginary, And yeah that is what i want , the “something” Can be a NPC
I have to run so I will not be able to type a draft script for you, but I can share my idea real quick. Let’s say the distance you want the NPC to be from the player is 15 studs. You will make an imaginary line (possibly a Ray) starting from the character and going out 15 studs. It will then face a random LookVector. Then, you teleport the NPC to the end of the imaginary line.
position = target_humanoidrootpart.Position + Vector3.new(
math.cos(angle * math.pi * 2),
0,
math.sin(angle * math.pi * 2)
) * radius
angle is 0 to 1 radius can be any distance u want
What exactly is the angle and radius?
angle = 0 - 1 is like 0 - 360 degrees
radius = from your drawing the radius is how far the blue block is form the red circle
So in your case, you would set angle to a random number between 0 and 1
Why when i change * 2 to 5 or any other number his position changes?
position = target_humanoidrootpart.Position + Vector3.new(
math.cos(angle * math.pi * 2),
0,
math.sin(angle * math.pi * 2)
) * radius
You don’t need to change that stuff, just need to make variables for angle and radius
I did this and didnt work
local angle = math.random(0,1)
local radius = 15
position = workspace.Dummy.HumanoidRootPart.Position + Vector3.new(
math.cos(angle * math.pi * 2),
0,
math.sin(angle * math.pi * 2)
) * radius
workspace.Part.Position = position
That script should make a part go somewhere random around the NPC. That does not move the NPC. That is what you intended?
local debris = game:GetService("Debris")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local vectors = {"rightVector", "lookVector"}
local scalars = {-5, 5}
while true do
local part = Instance.new("Part")
print(part)
part:PivotTo(character:GetPivot() * CFrame.Angles(0, math.rad(math.random(0, 360)), 0) + hrp.CFrame[vectors[math.random(#vectors)]] * scalars[math.random(#scalars)])
part.Parent = workspace
task.wait(1)
part:Destroy()
end
This is an example script similar to what you’re trying to achieve. It’s a local script placed inside the “StarterCharacterScripts” folder.
Randomly oriented BasePart instances are spawned in the four requested directions of the player’s character model.
That’s close, but rather than 4 points at 90 degrees each, he wants any random point. The picture was just an example.
behind or front or right or left of the Player?
I took his quote literally (the provided diagram seems to infer the same as well).
function idkhowtocallthisfunction(distance)
local degree = math.random(0,90)
local x = math.cos(math.rad(degree)) * distance
local z = (math.sqrt(1-((x/distance)*(x/distance))))*distance
local negativex = math.random(0,1)
local negativez = math.random(0,1)
if negativex == 1 then
x = -x
end
if negativez == 1 then
z = -z
end
return Vector3.new(x,0,z)
end
Now all you need to do is to add that Vector3 value to the player’s CFrame
It really doesnt matters that much if its random, but The scalars are the distance between the part and The character right?
Yes, -5 studs (behind/left) or 5 studs (front/right), that can be changed at your discretion.