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

Magnitude script isnt working? No error either?

Asked by 6 years ago

I just got into learning magnitude yesterday night in an effort to try to get something working.

My goal at the moment is to, well I am working on Darth Vader and I want him to choke people who are around him when he presses Q.

I am using magnitude so the script knows who is near him when he uses the move.

But I am a little lost, magnitude is something ive never worked with and all the tutorials on it are 4 years old or older.

So heres what ive got so far in my script but its not working nor does it error. Anyone know why.

I am trying to make it so if you are near Vader when he presses Q it basically kills the people around him except Vader himself.

Heres the script so far.

    local function onKeyPressed(inputObject, gameProcessedEvent)
        if inputObject.KeyCode == Enum.KeyCode.Q then
            local MainPlayer = Player.Character:WaitForChild("HumanoidRootPart")
            local OtherPlayer = game.Players:GetChildren()          

            for _, TargetPlayer in pairs(game.Players:GetChildren()) do 
                if not TargetPlayer == MainPlayer then 
                    local Magnitude = (MainPlayer.Position - TargetPlayer.Position).magnitude
                    if Magnitude <= 10 then
                        print("They are too close!")
                    end
                end
            end

        elseif inputObject.KeyCode == Enum.KeyCode.E then

        elseif inputObject.KeyCode == Enum.KeyCode.R then

        end
    end
    UserInputService.InputBegan:connect(onKeyPressed)

Im sorry the indentations are weird, its inside a onEquipped function inside a tool.

Thank you for your time. Also happy new year!

0
Who's ""MainPlayer?? Don't hurt him! D: TheeDeathCaster 2368 — 6y
0
Main player is defined at the top of the script. Its game.Players.LocalPlayer GottaHaveAFunTime 218 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Issue

You're iterating through all of the players. TargetPlayer doesn't have a Position property and thus cannot be subtracted with another Vector. However, you will be able to get the magnitude between TargetPlayer's Character's Position and your MainPlayer's position.

for _, TargetPlayer in pairs(game.Players:GetPlayers()) -- Refrain from using :GetChildren() when you only need an array of players.
    if not TargetPlayer == MainPlayer then
        local TargetCharacter = TargetPlayer.Character -- Get the player's character so we can use its position later.
        local Magnitude = (MainPlayer.Position - TargetCharacter.PrimaryPart.Position).magnitude
        if Magnitude <= 10 then
            print('They are too close!')
        end
    end
end

Explanation

What is a Vector?

A mathematical vector is simply an arrow in space, starting at one point and ending in another. It has a magnitude and a direction. The magnitude of a Vector is the 'size' or 'length' of that Vector. Roblox allows us to instantiate Vectors in R3 using the Vector3.new(x, y, z) constructor.

All vectors in roblox start at the origin (0, 0, 0), but in the real world its completely arbitrary. You can calculate the magnitude of a Vector using the pythagorean theorem, where the x and y component would be a and b. Luckily Vector3s already have a magnitude property, which calculates the distance from the origin to the tip of the Vector for us.

So how do we get the magnitude between two Vectors?

Simple, we can get the displacement (difference) between two vectors by subtracting them: (v2 - v1). Now we have a vector describing the displacement between these two vectors, we can get it's magnitude to find its size (more specifically the distance between the "displacement vector" and the origin): (v2 - v1).magnitude.

0
Wait it still doesnt work and there isnt an error? Do you have any ideas? If you want i can update the code to show my new code. GottaHaveAFunTime 218 — 6y
0
sure LifeInDevelopment 364 — 6y
Ad

Answer this question