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

Loading the player and calculating distances?

Asked by 8 years ago

The goal of this script is to spawn the player in, and re - spawn him or her again. The script is also supposed to print the player's distance between there torso and another part, if the player is still alive.

So the script works like this:

--First, I use a PlayerAdded event to detect when and what player has joined the game, then I use a CharacterAdded event on that player to detect what character the player is using.

--Next, I Load the character, and connect the 2nd function (also including the player and the character as arguments)

--The 2nd function will print the distance between the Torso of the player's current character, and the a part in the workspace (60 times per second), as long as the player and the character is other than nil.

Now I don't exactly know what the problem is, but the output errors this:

  • Torso is not a valid member of Model
  • Script 'Workspace.Script', Line 5 - global FindDistance
  • Script 'Workspace.Script', Line 17
  • Stack End

Here's the script:

function FindDistance(player,character)
    while player ~= nil and character ~= nil and character.Humanoid.Health ~= 0 do
        wait()
        if character ~= nil then
            print((character.Torso.CFrame.p - workspace.Cube.Position).magnitude)
        end
    end
end

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:connect(function(player)
    local HoldVar = 0
    player.CharacterAdded:connect(function(character)
        if HoldVar == 0 then
            HoldVar = 1
            FindDistance(player,player.Character)
        elseif HoldVar == 1 then
            FindDistance(player,character)
        end
        character.Humanoid.Died:connect(function()
            wait(2.25)
            player:LoadCharacter()
            repeat wait() until character.Torso ~= nil
            character.Torso.CFrame = CFrame.new(workspace.Spawn.Position) + Vector3.new(0,10,0)
            FindDistance(player,character)
        end)
    end)
    player:LoadCharacter() --this loads the player for when he first joins
    player.Character.Torso.CFrame = CFrame.new(workspace.Spawn.Position) + Vector3.new(0,10,0)
end)

1 answer

Log in to vote
0
Answered by 8 years ago

It appears that for whatever reason the Torso object is not created before CharacterAdded is fired. You can fix that by changing line 5 to print((character:WaitForChild("Torso").CFrame.p - workspace.Cube.Position).magnitude) I also suggest changing line 24 too character:WaitForChild("Torso")

0
But the loop should stop because the character is nil, right? LateralLace 297 — 8y
0
If the character parameter were nil then yes the loop would stop. NullSenseStudio 342 — 8y
Ad

Answer this question