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

How to make teleporting have an offset? [Solved]

My goal here is to make a handcuff system, where when the officer clicks a suspect, the suspect teleports to his hands hand he is sort of welded to the officer and has no control. My problem is that when I try to teleport the suspect to the officer with the offset script I've already made, it sometimes goes behind the officer instead of where I want him to go, which is in the front to the side a bit.

any help would be appreciated.

(first local script)

local tool = script.Parent
local players = game:GetService("Players")
tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			if mouse.Target.Parent:FindFirstChild("Humanoid") then
				game:GetService("ReplicatedStorage"):WaitForChild("ArrestGrab"):FireServer(mouse.Target.Parent)
			end
		end
	end)
end)

(Second script from server)

game:GetService("ReplicatedStorage"):WaitForChild("ArrestGrab").OnServerEvent:Connect(function(officer,suspect)
	if suspect.Settings.inCuffs.Value ~= true then
		local officerChar = officer.Character
		local a,b,c = officerChar.HumanoidRootPart.Position.X-1.5,officerChar.HumanoidRootPart.Position.Y,officerChar.HumanoidRootPart.Position.Z+1.5
		suspect.HumanoidRootPart.Position = Vector3.new(a,b,c)
		officerChar.HumanoidRootPart.Orientation = 	suspect.HumanoidRootPart.Orientation
		suspect:WaitForChild("Humanoid"):LoadAnimation(game:GetService("ServerStorage"):WaitForChild("Grab")):Play()
		suspect:WaitForChild("Humanoid"):LoadAnimation(game:GetService("ServerStorage"):WaitForChild("Arrest")):Play()
	end
end)

(please ignore the un-used functions and such, I removed those bits to give a better understanding of what the actual problem is)

2 answers

✓ Accepted answer

Using position and orientation wouldn't be the best way to go, what you should use is something called CFrame. CFrame stands for Coordinate Frame and it accounts for a part's position in a 3-D axis as well as its rotation! You've got a 2-in-1 package to solve your problem :D

Here's what the second script looks like:

game:GetService("ReplicatedStorage"):WaitForChild("ArrestGrab").OnServerEvent:Connect(function(officer, suspect)
    if suspect.Settings.inCuffs.Value ~= true then
		local officerChar = officer.Character
		
	    suspect.HumanoidRootPart.CFrame = officerChar.HumanoidRootPart.CFrame * CFrame.new(-1.5, 0, -2)
		    --This teleports the suspect 1.5 studs to the left of the officer, and 2 studs in front

		suspect:WaitForChild("Humanoid"):LoadAnimation(game:GetService("ServerStorage"):WaitForChild("Grab")):Play()
		suspect:WaitForChild("Humanoid"):LoadAnimation(game:GetService("ServerStorage"):WaitForChild("Arrest")):Play()
    end
end)

Explanation You are teleporting the suspect's HumanoidRootPart to the officer's HumanoidRootPart, and the Offset is expressed by multiplying the CFrame by CFrame.new(x, y, z), each corresponding to a stud measure. Rather than using Positions, which uses a global axis for positioning, use CFrame to express the local positioning of parts non-relative to the world!

I hope this helps :) HMU with any problems and questions

In case anybody has the same question, I figured it out myself by using CFrame and not Position.

1 comment

0

o

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