Players not teleporting

I’m trying to make an ability in my game where the player using it swaps positions with the nearest player, but it won’t teleport. It does grab the positions of the players, however it just won’t swap them, here’s the script, if you have anything to help please let me know:

local plrs = game:GetService("Players")
local maxd = 10

game.ReplicatedStorage.AbilityEvents.Biar.OnServerEvent:Connect(function(plr)
	local hrp = plr.Character.HumanoidRootPart
	local nearestplr, nearestdistance
	for _, player in pairs(plrs:GetPlayers()) do
		if player.Name ~= plr.Name then
			local vchar = player.Character
			local distance = player:DistanceFromCharacter(hrp.Position)
			if not vchar or distance > maxd or (nearestdistance and distance >= nearestdistance) then
				continue
			end
			nearestdistance = distance
			nearestplr = player
			local hrppos = hrp.Position
			local charpos = nearestplr.Character.HumanoidRootPart.Position
			hrppos = charpos
			charpos = hrppos
		end
	end
end)
local plrs = game:GetService("Players")
local maxd = 10

game.ReplicatedStorage.AbilityEvents.Biar.OnServerEvent:Connect(function(plr)
	local hrp = plr.Character.HumanoidRootPart
	local nearestplr, nearestdistance
	for _, player in pairs(plrs:GetPlayers()) do
		if player.Name ~= plr.Name then
			local vchar = player.Character
			local distance = player:DistanceFromCharacter(hrp.Position)
			if not vchar or distance > maxd or (nearestdistance and distance >= nearestdistance) then
				continue
			end
			nearestdistance = distance
			nearestplr = player
			local hrppos = hrp.Position
			local charpos = nearestplr.Character.HumanoidRootPart.Position
			hrp.Position = charpos
			nearestplr.Character.HumanoidRootPart.Position = hrppos
		end
	end
end)

This should fix the problem in your script.

If you define the value of a variable to be a property, if you change the value it will not change the property value.

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