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

Random Teleport teleporting me to wrong place. Please help?

I want my script to teleport me to the "Hostile District" (A place I built in my game) but it teleports me to someplace in the air and far away from where I should teleport.

function onTouch(hit)
local s=math.random(1,2)
if s == 1 then
hit.Parent:MoveTo(Vector3.new(workspace.HostileDistrict.TeleLoc.Position)) -- Hostile District
--elseif s == 2 then
--hit.Parent:MoveTo(Vector3.new(-461.15, 2.05, -11125.091)) -- Sand Box
end
end

script.Parent.Touched:Connect(onTouch)

And when I do it like this:

function onTouch(hit)
local s=math.random(1,2)
if s == 1 then
hit.Parent:MoveTo(Vector3.new(1147.749, -429.55, -103.099)) -- Hostile District
--elseif s == 2 then
--hit.Parent:MoveTo(Vector3.new(-461.15, 2.05, -11125.091)) -- Sand Box
end
end

script.Parent.Touched:Connect(onTouch)

It teleports me a few feet in the air above where I want it.

btw, I've disabled lines 5 and 6 by turning them into comments so it teleports me directly to the Hostile District so I can test its teleport. As for the Sand Box however, it teleports me exactly to the correct spot. Can someone help me with this?

P.S: I tried moving the location then rewriting the position, but it still has the same problem.

2 answers

###Using MoveTo to move a model can result in weird behaviour, such as moving to the wrong place. Character models have a PrimaryPart, and you can set the CFrame of the primary part to the position.

function onTouch(part)
    if part.Parent:FindFirstChild"HumanoidRootPart" then
        part.Parent:SetPrimaryPartCFrame(CFrame.new(1147.749, -429.55, -103.099))
    end
end

1 comment

0

Nothing happens.

When you move an object using Vector3 it will not clip through other objects. You will instead spawn wherever you need to be to not clip through something, generally above the desired position. You might want to try using CFrame by using :SetPrimaryPartCFrame. Assuming its a player, the player model will already have the PrimaryPartCFrame as HumanoidRootPart. Thus all you need to do is this.

function onTouch(hit)
	local s=math.random(1,2)
	if s == 1 then
	hit.Parent:SetPrimaryPartCFrame(CFrame.new(1147.749, -429.55, -103.099)) -- Hostile District
	--elseif s == 2 then
	--hit.Parent:SetPrimaryPartCFrame(CFrame.new(-461.15, 2.05, -11125.091)) -- Sand Box
	end
	end
script.Parent.Touched:Connect(onTouch)

This will error if an object that isn't a player touches. So if you don't add a check to see if an object touching is a player, it will break. I'll leave that up to you to figure out, it is quite simple. Anyway, I hope this helps! If it doesn't my discord is Autizmos#3556 and i'll be happy to assist you!

2 comments

0

Ok, I've added you on discord, cause the script teleported me outside of the Hostile District.

0

I apologize, i accidentally declined and sent a request back. I'll gladly help when you accept

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