Switch Player's Places Tool

I’m trying to make a tool that switches the places of 2 players - the one using the tool and the player their mouse is pointing at.

The one problem that I have is that the position of the players doesn’t change but it is doing something strange (as you can see in the video below)

This is the server side code that handles the teleportation

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemotesRE = ReplicatedStorage.Remotes.SwapRE
local MAX_DISTANCE = 50

RemotesRE.OnServerEvent:Connect(function(Player, Victim)
	local char = Player.Character
	local hum = char:FindFirstChild("Humanoid")
	local hrp = char:FindFirstChild("HumanoidRootPart")
	
	local Vchar = Victim.Character
	local Vhum = Vchar:FindFirstChild("Humanoid")
	local Vhrp = Vchar:FindFirstChild("HumanoidRootPart")
	
	local PlayerPosition = hrp.Position
	local VictimPosition = Vhrp.Position
	
	if not (hrp and Vhrp and hum and Vhum) then return end
	if hum.Health <= 0 or Vhum.Health <= 0 then return end
	
	if (hrp.Position - Vhrp.Position).Magnitude <= MAX_DISTANCE then
		local aux = char:GetPivot()
		char:PivotTo(Vchar:GetPivot())
		Vchar:PivotTo(aux)
		print("Teleported")
	end
end)

And also, another issue, with the logic that I’m trying to use now, wouldn’t the first player that is teleporting going inside the other player’s character and glitching, maybe even getting pushed? Cause I wouldn’t like that. I would’ve tested that already if the tp script even worked, but if that is really the case what should I do? I guess just turn the characters’ body parts cancollide property off and after the tp on. Let me know what you think

well to stop the collision issue you can turn off collision on the hrp as for the teleporting try the char:MoveTo() function

1 Like

This is what it does now

are you using the MoveTo function on the character?

1 Like

Yeah, you can see the script at the end of the video

weird try printing the players name and the victims name you might need to add a check on the server and client for the tool

1 Like

also can I see the localscript?

1 Like

I’m actually dumb. When firing the server I was giving the player and the victim forgetting that when doing onserverevent you already get the player as a param. So basically in the server script both the “Player” and the “Victim” were the player. Fixed it just now. Thank you so much!

1 Like

ah, ok I figured you were somehow getting the same player. No problem man!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.