Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How come im unable to calculate a players distance from a player they killed?

I'm trying to calculate a player's distance from a player they killed but it won't work, and when I check the error it only confuses me more.

Version 1

--###----------[[SERVICES]]----------###--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--###----------[[KILLHANDLER]]----------###--
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			print("Player Killed").
			local tag = character.Humanoid:FindFirstChild("creator")
			if tag ~= nil then
				local Player = tag.value
local PlayerCharacter = Player.Character
				local Distance = (PlayerCharacter.HumanoidRootPart.Position - character.HumanoidRootPart.Position).magnitude
				print(Distance)
				Player.leaderstats.Kills.Value += 1
				ReplicatedStorage.EXPEvent:FireServer(player.Name)
				print("Kill Added")
			end
		end)
	end)
end)

Version 1 Error

HumanoidRootPart is not a valid member of Model "Workspace.Player2"

Version 2

--###----------[[SERVICES]]----------###--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--###----------[[KILLHANDLER]]----------###--
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			print("Player Killed")
			local tag = character.Humanoid:FindFirstChild("creator")
			if tag ~= nil then
				local Killer = tag.Value
				local Distance = player:DistanceFromCharacter(Killer.HumanoidRootPart.Position)
				print(Distance)
				Killer.leaderstats.Kills.Value += 1
				ReplicatedStorage.EXPEvent:FireServer(player.Name)
				print("Kill Added")
			end
		end)
	end)
end)

WARNING

I should have said this beforehand but the entire code is inside a ServerScript that is inside of ServerScriptService

2 answers

To calculate length between parts to parts with vector, use .Magnitude! For example:

local Distance = (Part1.Position - Part2.Position).Magnitude
print(Distance) -- The distance between Part1 to Part2 in studs

So line 13 should be:

 local Distance = (PlayerCharacter.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude

maybe the error is another issue, i dont really know how to fix it

1 comment

0

I do know the error is from line 13, but when I put the ".magnitude" I still get the error from line 13(I should have said what line its from))

You can use

player:DistanceFromCharacter(OtherPlayer)

Instead of using magnitude

3 comments

0

Almost, the function requires a Vector3, not an instance. So you could do i.e OtherPlayer.Head.Position

0

So like this "local Distance = player:DistanceFromCharacter(Killer.HumanoidRootPart.Position)"?

0

Also the script is a serverscript

This discussion is preserved as it was. New answers, comments, and votes are closed.