How do I make my swap ability pick people that are close to my Y level? [SOLVED]

  1. What do you want to achieve? I want to make swap system pick random players beside me, and pick only people that are on the same Y level as me, or my Y position + 7 (close to jump height).

  2. What is the issue? The system only picks random players beside me, but sometimes it picks a player that is way above me or below me.

  3. What solutions have you tried so far? I tried to make it in repeat loop, but I didn’t know how to make the code stick together with this loop.

Here is my current code:

elseif AbilityValue == "Swap"  then
		cooldown.Value = 20
		---------------------
		local Players = game:GetService("Players")
		local PlayerList = Players:GetPlayers()

		table.remove(PlayerList, table.find(PlayerList, plr)) -- removes player from the table.

		local randomplr = PlayerList[math.random(#PlayerList)].Name -- picks random player, (currently all players beside me, and I want it to pick player close to my Y level)
		
--vfx stuff
		local beam = AbilityAssets.Swap.Part.Beam:Clone()
		local bigbeam1 = AbilityAssets.Swap.Part.BIGBEAM1:Clone()
		local bigbeam2 = AbilityAssets.Swap.Part.BIGBEAM2:Clone()
		bigbeam1.Parent = Players[randomplr].Character.HumanoidRootPart
		bigbeam2.Parent = plr.Character.HumanoidRootPart
		beam.Parent = plr.Character.HumanoidRootPart
		beam.Attachment0 = bigbeam1
		beam.Attachment1 = bigbeam2
		
		task.wait(1)
		
--Gets random player's Y level and the first player's

		local Y0 = plr.Character:GetPivot().Position.Y 
		local Y1 = Players[randomplr].Character:GetPivot().Position.Y
		
		local min = Y0-1
		local max = Y0+7

-- making the script work only if the random player is close to my Y level
		if Y1 <= max and Y1 >= min or Y1 == Y0 then 
			print("can swap")
			local position0 = plr.Character.HumanoidRootPart.Position
			local position1 = Players[randomplr].Character.HumanoidRootPart.Position
			beam:Destroy()
			bigbeam1:Destroy()
			bigbeam2:Destroy()
			plr.Character.HumanoidRootPart.Position = position1
			Players[randomplr].Character.HumanoidRootPart.Position = position0
		else
			print("can't swap "..Y0.." "..Y1)
			beam:Destroy()
			bigbeam1:Destroy()
			bigbeam2:Destroy()
		end

I’m counting for someone to help me! :slightly_smiling_face:

1 Like

the problem is that you checked for only 1 player and if he is far then the script will continue without checking if other players are close to Y

this code checks for every player and see if they are close to Y level and if they are the loop will stop though I cannot test it so am not sure if it will work

for _, player2 in PlayerList do
		local Y0 = plr.Character:GetPivot().Position.Y 
		local Y1 = player2.Character:GetPivot().Position.Y

		local min = Y0-1
		local max = Y0+7

		-- making the script work only if the random player is close to my Y level
		if Y1 <= max and Y1 >= min or Y1 == Y0 then 
			print("can swap")
			local position0 = plr.Character.HumanoidRootPart.Position
			local position1 = player2.Character.HumanoidRootPart.Position
			beam:Destroy()
			bigbeam1:Destroy()
			bigbeam2:Destroy()
			plr.Character.HumanoidRootPart.Position = position1
			Players[randomplr].Character.HumanoidRootPart.Position = position0
			break -- stop the loop if a player is found
		else
			print("can't swap "..Y0.." "..Y1)
			beam:Destroy()
			bigbeam1:Destroy()
			bigbeam2:Destroy()
		end
	end
3 Likes

I think it’s working, but sometimes it just doesn’t work, i mean by if i’m with my friend on the same level, in the output it says we are on different Y position. Help me figure it out please

	elseif AbilityValue == "Swap"  then
		cooldown.Value = 20
		---------------------
		local Players = game:GetService("Players")
		local PlayerList = Players:GetPlayers()

		table.remove(PlayerList, table.find(PlayerList, plr)) -- woops!

		local randomplr = PlayerList[math.random(#PlayerList)].Name
		
		local beam = AbilityAssets.Swap.Part.Beam:Clone()
		local bigbeam1 = AbilityAssets.Swap.Part.BIGBEAM1:Clone()
		local bigbeam2 = AbilityAssets.Swap.Part.BIGBEAM2:Clone()
		local circleStart1 = AbilityAssets.Swap.Part.CircleStart:Clone()
		local circleStart2 = AbilityAssets.Swap.Part.CircleStart:Clone()
		local circleEnd1 = AbilityAssets.Swap.Part.CircleEnd:Clone()
		local circleEnd2 = AbilityAssets.Swap.Part.CircleEnd:Clone()
		bigbeam1.Parent = Players[randomplr].Character.HumanoidRootPart
		bigbeam2.Parent = plr.Character.HumanoidRootPart
		
		circleStart1.Parent = plr.Character.HumanoidRootPart
		circleEnd1.Parent = plr.Character.HumanoidRootPart
		circleStart2.Parent = Players[randomplr].Character.HumanoidRootPart
		circleEnd2.Parent = Players[randomplr].Character.HumanoidRootPart
		
		beam.Parent = plr.Character.HumanoidRootPart
		beam.Attachment0 = bigbeam1
		beam.Attachment1 = bigbeam2
		
		task.wait(1)
		for _, player2 in PlayerList do
			local Y0 = plr.Character:GetPivot().Position.Y 
			local Y1 = player2.Character:GetPivot().Position.Y

			-- making the script work only if the random player is close to my Y level
			if Y1 <= tonumber(plr.Character:GetPivot().Position.Y + 7) and Y1 >= tonumber(plr.Character:GetPivot().Position.Y + -1) or Y1 == Y0 then 
				print("can swap")
				local position0 = plr.Character.HumanoidRootPart.Position
				local position1 = player2.Character.HumanoidRootPart.Position
				beam:Destroy()
				bigbeam1:Destroy()
				bigbeam2:Destroy()
				circleStart1:Destroy()
				circleStart2:Destroy()
				circleEnd1:Destroy()
				circleEnd2:Destroy()
				plr.Character.HumanoidRootPart.Position = position1
				Players[randomplr].Character.HumanoidRootPart.Position = position0
				break -- stop the loop if a player is found
			else
				print("can't swap "..Y0.." "..Y1)
				beam:Destroy()
				bigbeam1:Destroy()
				bigbeam2:Destroy()
				circleStart1:Destroy()
				circleStart2:Destroy()
				circleEnd1:Destroy()
				circleEnd2:Destroy()
			end
		end
        end
2 Likes

could it be caused by this? (in the output it’s adding position)
if Y1 <= tonumber(plr.Character:GetPivot().Position.Y + 7)

1 Like

y position will often not be same because of floating point precision errors so you will have to check if the Y range between them is smaller than 7

am not sure if this is the reason but it may be because of the check in the if statement this is a modified version that may work
math.abs(n1 - n2) returns the difference from n1 and n2 we can use that to check if the difference between Y0 and Y1 is smaller than 7

for _, player2 in PlayerList do
	local Y0 = plr.Character:GetPivot().Position.Y 
	local Y1 = player2.Character:GetPivot().Position.Y

	-- making the script work only if the random player is close to my Y level
	if math.abs(Y0 - Y1) <= 7 then 
		print("can swap")
		local position0 = plr.Character.HumanoidRootPart.Position
		local position1 = player2.Character.HumanoidRootPart.Position
		beam:Destroy()
		bigbeam1:Destroy()
		bigbeam2:Destroy()
		circleStart1:Destroy()
		circleStart2:Destroy()
		circleEnd1:Destroy()
		circleEnd2:Destroy()
		plr.Character.HumanoidRootPart.Position = position1
		Players[randomplr].Character.HumanoidRootPart.Position = position0
		break -- stop the loop if a player is found
	else
1 Like

Oh my god, thanks it finally works!!!
Really appreciate the support, thank you very much :grin:

1 Like