Making swap ability

I am trying to make an ability like Boogie Woogie from JJK which switches your position with you target

There are 3 problems

  1. Everytime I try to switch it only switches the positions on the local side and not the server side
  2. brings them to my position but it does not bring me to their position
  3. Does not bring the hitbox with them

for 2 I tried putting the go to their position code before the come to my position code but that caused a even worse problem where it would not even bring them to my position
for 3 I tried making the hitbox also teleport to your position but that caused a problem where if you swapped in air the hitbox would stay above the target until you swapped again

I tried looking in the forum for this but I had no luck finding anything

You need to save the player’s locations before swapping either of them and you need to use PivotTo so the hitbox moves with the players. I can edit for you if you want but please send text version of the code

local UIS = game:GetService(“UserInputService”)
local RS = game:GetService(“ReplicatedStorage”)
local Switch = RS:WaitForChild(“Switch”)

Switch.OnServerEvent:Connect(function(player, target)
local playerPosition = player.Character.HumanoidRootPart.Position
local humanoidRootPart

if target.Parent:IsA("Model") and target.Parent:FindFirstChild("Humanoid") then
	humanoidRootPart = target.Parent:FindFirstChild("HumanoidRootPart")
elseif target.Parent:IsA("Accessory") then
	humanoidRootPart = target.Parent.Parent:FindFirstChild("HumanoidRootPart")
end 
local humanoidRootPartPosition = humanoidRootPart.Position
if humanoidRootPart then
	
	humanoidRootPart.Position = playerPosition
	if humanoidRootPart.Position == playerPosition then
		playerPosition = humanoidRootPart.Position 
	end
else
	return
end

end)

Here is the text version of the code, sorry I forgot it

I also tried your recommendation on using CFrames and I got the same problem
Here is what I tried

local UIS = game:GetService(“UserInputService”)
local RS = game:GetService(“ReplicatedStorage”)
local Switch = RS:WaitForChild(“Switch”)
Switch.OnServerEvent:Connect(function(player, target)
local playerPosition = player.Character.HumanoidRootPart.CFrame
local humanoidRootPart

if target.Parent:IsA("Model") and target.Parent:FindFirstChild("Humanoid") then
	humanoidRootPart = target.Parent:FindFirstChild("HumanoidRootPart")
elseif target.Parent:IsA("Accessory") then
	humanoidRootPart = target.Parent.Parent:FindFirstChild("HumanoidRootPart")
end 
local humanoidRootPartPosition = humanoidRootPart.Position
if humanoidRootPart then
	humanoidRootPart:PivotTo(playerPosition)
	humanoidRootPart.CFrame = playerPosition
	if humanoidRootPart.CFrame == playerPosition then
		playerPosition = humanoidRootPart.CFrame 
	end
else
	return
end

end)

I revamped your code a tiny bit. Here:

local UIS = game:GetService("“UserInputService”")
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Switch = RS:WaitForChild("Switch")

Switch.OnServerEvent:Connect(function(player, target)
	local PlayerCharacter = player.Character
	if not PlayerCharacter then return end
	
	local Player2 = Players:GetPlayerFromCharacter(target.Parent)
	if not Player2 then return end
	
	local TargetCharacter = Player2.Character
	if not TargetCharacter then return end
	
	local Hrp1 = PlayerCharacter:FindFirstChild("HumanoidRootPart")
	local Hrp2 = TargetCharacter:FindFirstChild("HumanoidRootPart")
	
	if not Hrp1 or not Hrp2 then return end
	
	local SavedPos1 = Hrp1.CFrame
	local SavedPos2 = Hrp2.CFrame
	
	PlayerCharacter:PivotTo(SavedPos2)
	TargetCharacter:PivotTo(SavedPos1)
end)

It now does not switch at all. Still your probably right about the CFrame and Saving Position Before thing

The reason why it didn’t work is because it was meant for players only. I now made it so it can work for NPC’s

New code:

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Switch = RS:WaitForChild("Switch")

Switch.OnServerEvent:Connect(function(Player:Player, target:BasePart)
	if not target then return end
	
	local PlayerCharacter = Player.Character
	if not PlayerCharacter then return end

	local TargetCharacter = target:FindFirstAncestorWhichIsA("Model")
	
	if not TargetCharacter or not TargetCharacter:FindFirstChildOfClass("Humanoid") then return end
	
	local Hrp1 = PlayerCharacter.PrimaryPart
	local Hrp2 = TargetCharacter.PrimaryPart

	if not Hrp1 or not Hrp2 then return end

	local SavedPos1 = Hrp1.CFrame
	local SavedPos2 = Hrp2.CFrame

	PlayerCharacter:PivotTo(SavedPos2)
	TargetCharacter:PivotTo(SavedPos1)
end)

Thank you, I was trying to solve this for a while.
There is still a problem where the hitbox raises a bit but it is only with the ai and I am pretty sure I can fix that by myself

If you continue to have issues with your hitbox you’re free to ask for help

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