Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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

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)) vincentthecat1 199 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You can use

player:DistanceFromCharacter(OtherPlayer)

Instead of using magnitude

0
Almost, the function requires a Vector3, not an instance. So you could do i.e OtherPlayer.Head.Position the8bitdude11 358 — 3y
0
So like this "local Distance = player:DistanceFromCharacter(Killer.HumanoidRootPart.Position)"? vincentthecat1 199 — 3y
0
Also the script is a serverscript vincentthecat1 199 — 3y

Answer this question