player:GetDistanceFromCharacter() not working?

Hello.
I was working on a game that I am getting paid for, and I have ran into an issue here.

I am developing a giant, that launches players into the air, and then eats them.
Game Link: Map - Roblox

GIANT

The problem is, that when the player gets eaten while he has just spawned, and is immune, then he will not be eaten again.

I think, that the problem has something to do with the distance checking:

Distance normally, when close to giant:

60
59
58
50

Distance after trying to be eaten, when close to giant:

80
70
68
60

Main Script

local humanoid = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("UpperTorso")

local animate = require(script.Parent.Animate)

local jump = false
local killedPlayers = {}

local function jump()
	
	if not jump then
		
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		jump = true
		
		humanoid.StateChanged:Wait()
		jump = false
		
	end
	
end

local function closestPlayer()
	
	local distances = {}
	local lowest = math.huge
	
	local player
	
	for _, player in pairs(game.Players:GetPlayers()) do
		
		local humanoid = player.Character:FindFirstChild("Humanoid")
		if humanoid and humanoid.Health > 0 then
			
			distances[player] = player:DistanceFromCharacter(torso.Position)
			
		end
		
	end
	
	for i, v in pairs(distances) do
		
		if v and v < lowest then
			
			lowest = i
			
		end
		
	end
	
	for i, v in pairs(distances) do
		
		if v and v == distances[lowest] then
			
			player = i
			
		end
		
	end
	
	if player then return player.Character:FindFirstChild("HumanoidRootPart"), distances[player] end
	
end

while true do
	
	local root, distance = closestPlayer()
	if root and distance then
		
		humanoid:MoveTo(root.Position)
		if distance <= 45 then
			
			animate.stompAnimation(root)
			
			humanoid:MoveTo(torso.Position)

			task.wait(4.1)
			
		end
		animate.runAnimation()
		
	else
		
		humanoid:MoveTo(torso.Position)
		
	end
	
	task.wait()
	
end

I’m not certain if this is causing you problems or not… But there are two different “player” variables here. One is outside the scope of the loop and one is inside the scope of the loop.

1 Like

That could have been a mistake, but it still doesn’t fix the problem. For some reason, the distance from the closest players HumanoidRootPart increases, after the giant tried to eat him, but failed, because the player had just spawned.

Here is the problem, portrayed by print() debugging.

The upper half is from before the giant lifted the player up, and the bottom one, right after the giant spits the player out.

I will also leave a game link in the thread description.

Distance from the player
image