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
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
You can use
player:DistanceFromCharacter(OtherPlayer)
Instead of using magnitude