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

Fling all players in a server script?

Asked by
JSAcela 25
7 years ago

Here's what i've tried so far:

for _,P in pairs(game.Players:GetPlayers()) do
if P.Character then
     local t=P.Character:FindFirstChild("Humanoid") 
    if t then 
        t.Parent.Torso.Velocity = Vector3.new(300,5,25)
    end
end
end

but this only seems to fling one player. Could somebody possibly modify it to make it fling all the players in the server instead of just one? I am stumped here and I have been working on it for almost 2 weeks!

Thanks for your help!

1 answer

Log in to vote
1
Answered by
tkcmdr 341 Moderation Voter
7 years ago
Edited 7 years ago

Hey JSAcela,

There isn't any immediately noticeable problem with your code, but I have noted that you do not check to make sure that the Torso of the current player actually exists. If the current player does not have a Torso, your code will immediately break. Try something like this:

for i, player in pairs(game.Players:GetPlayers()) do
    if player.Character then
        local torso = player.Character:FindFirstChild("Torso");

        if (torso) then 
            torso.Velocity = Vector3.new(300,5,25);
        end;
    end;
end;

I did notice how you didn't do a very good job indenting and using descriptive variable names. I highly recommend reading this very informative and useful blog post by NoahWillCode:

https://scriptinghelpers.org/blog/clean-and-tidy

Have a nice day, JSAcela, and best of luck with your project!

Cheers,

tkcmdr

Edit:

Here is a new bit of code that will fling a random player:

-- Returns a list of characters with a torso.
local function GetCharacters()
    local characters = {};

    for i, player in pairs(game.Players:GetPlayers()) do
        local isValidCharacter = (
            player.Character and
            player.Character:FindFirstChild("Torso")
        );

        if (isValidCharacter) then
            table.insert(characters, player.Character);
        end;
    end;

    return characters;
end;

local function FlingRandomPlayer()
    local characters    = GetCharacters();
    local victim        = characters[math.random(1, #characters)];

    if (victim) then
        victim.Torso.Velocity = Vector3.new(300,5,25);
    end;
end;

-- ...

-- Fling a random player.
FlingRandomPlayer();
0
Thank you very much for your help! I'm going to give this script a shot. Basically what happens when the script is run is that it flings a random player instead of all of the players. Thanks for your response! JSAcela 25 — 7y
0
Oh, in that case, I'll put edit my answer and add a new bit of code for you to use. Have a look! tkcmdr 341 — 7y
0
Don't forget to mark this question as answered, bud! Have a nice day! c; tkcmdr 341 — 7y
Ad

Answer this question