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

How to make teleporting have an offset? [Solved]

Asked by
rexhawk 222 Moderation Voter
6 years ago
Edited 6 years ago

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)

01local tool = script.Parent
02local players = game:GetService("Players")
03tool.Equipped:Connect(function(mouse)
04    mouse.Button1Down:Connect(function()
05        if mouse.Target and mouse.Target.Parent then
06            if mouse.Target.Parent:FindFirstChild("Humanoid") then
07                game:GetService("ReplicatedStorage"):WaitForChild("ArrestGrab"):FireServer(mouse.Target.Parent)
08            end
09        end
10    end)
11end)

(Second script from server)

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

(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

Log in to vote
0
Answered by 6 years ago

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:

01game:GetService("ReplicatedStorage"):WaitForChild("ArrestGrab").OnServerEvent:Connect(function(officer, suspect)
02    if suspect.Settings.inCuffs.Value ~= true then
03        local officerChar = officer.Character
04 
05        suspect.HumanoidRootPart.CFrame = officerChar.HumanoidRootPart.CFrame * CFrame.new(-1.5, 0, -2)
06            --This teleports the suspect 1.5 studs to the left of the officer, and 2 studs in front
07 
08        suspect:WaitForChild("Humanoid"):LoadAnimation(game:GetService("ServerStorage"):WaitForChild("Grab")):Play()
09        suspect:WaitForChild("Humanoid"):LoadAnimation(game:GetService("ServerStorage"):WaitForChild("Arrest")):Play()
10    end
11end)

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

Ad
Log in to vote
0
Answered by
rexhawk 222 Moderation Voter
6 years ago

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

0
o Awesom3_Eric 157 — 6y

Answer this question