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

How can our player affect an other player?

So how can we effect another player so for example if u don't get what i mean is that when we kill an player with sword or something else it effects them because they get killed on their screen and become 0 hp. So how can i can effect someone. For example if i created a gui that would kill a player when clicked the button who ever is close to that guy who has the kill gui it would affect them and kill that player that the person want to. Please tell me how can i do it. Not how to do the kill gui i want to know how can our player effect other player? Do we use remote events or something else if so how?

1 answer

Well, I can help you with how can you get the closest player using magnitude. Here's a sample code and all explanation in it :

-- It's a Local Script

while wait(10) do
    local ClosestPlayer -- Creating a nil var
    local ClosestChar -- Creating a nil var
    local Char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait() -- Waits for whole character to load in
    local CharPos = Char.PrimaryPart.Position -- The position of our character

    for i, player in pairs(game.Players:GetPlayers()) do -- Looping through player's table
        if player ~= game.Players.LocalPlayer then -- It is to make sure that the code doesn't apply to own player
            local plr = player.Character or player.CharacterAdded:Wait() -- Waiting for that player's character to load in
            if (CharPos - plr.PrimaryPart.Position).magnitude <= 30 then -- Making sure that the code will run, when the player is 30 studs or less away from our player
                if ClosestPlayer == nil then -- If your variable is nil
                    ClosestPlayer = player -- Assigns variable to the player closest found
                    ClosestChar = player.Character or player.CharacterAdded:Wait()
                else
                    if (CharPos - plr.PrimaryPart.Position).magnitude < (ClosestChar.PrimaryPart.Position - CharPos).magnitude then -- If another player is more closer than the previous player
                        ClosestPlayer = player -- Assigns the variable to that player
                        ClosestChar = player.Character or player.CharacterAdded:Wait()
                    end
                end
            end    
        end    
    end
    
    if ClosestPlayer ~= nil then -- If there is closest player
        print(ClosestPlayer.Name, (ClosestChar.PrimaryPart.Position - CharPos).magnitude) -- Prints the information, about, the name of the player and how many studs he/she is away from our player
    end    
end    

Now, you know how to get the closest player, you can continue your code.

Lemme know if it helps!

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