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

Why will this script not choose a random player and then teleport them to a brick in game.Workspace?

Asked by
jarat7 0
9 years ago
local Fightera = game.Players[math.random(1, #game.Players:GetChildren())]
Fighter1 = game.Players.Fightera.Name
local Fighterb = game.Players[math.random(1, #game.Players:GetChildren())]
Fighter2 = game.Players.Fighterb.Name
if Fighter1.Name ~= Fighter1.Name and Fighter2.Name ~= Fighter1.Name then
    Message.Text = (""..Fightera..""..Fighterb..". Thank you for playing.")
    game.Workspace.Fightera.Position = game.Workspace.yy.Position
    game.Workspace.Fighterb.Position = game.Workspace.yy.Position

For the sake of testing, I just have a part in Workspace called 'yy' to teleport both of the players in the game. I cannot however get it to pick two random people. It doesn't even change the messages text to what it is supposed to. What am I doing wrong? I have it finding a random player with Fighter1 and Fighter2, then I have Fightera and Fighterb just to find their names. Please help!

0
Look closer at the code; You have errors throughout Lines 1-4. TheeDeathCaster 2368 — 9y

2 answers

Log in to vote
0
Answered by
SurVur 86
9 years ago
local Fightera = game.Players[math.random(1, #game.Players:GetChildren())]
Fighter1 = game.Players.Fightera.Name
local Fighterb = game.Players[math.random(1, #game.Players:GetChildren())]
Fighter2 = game.Players.Fighterb.Name
if Fighter1.Name ~= Fighter1.Name and Fighter2.Name ~= Fighter1.Name then
    Message.Text = (""..Fighter1..""..Fighter2.." Thank you for playing.")
    game.Workspace.Fightera.Character.Torso.CFrame = game.Workspace.yy.CFrame
    game.Workspace.Fighterb.Character.Torso.CFrame = game.Workspace.yy.CFrame

You tried to use Fightera and Fighterb as a string, but they're actually instances. You just meant to use Fighter1 and Fighter2. You also tried to "move" the actual player instance. You meant to move the character. I do not use vector3s to move characters, instead I just move the cframe of their torsos.

Ad
Log in to vote
0
Answered by 9 years ago

Your problem is Lines 1-4; You are attempting to get the names of Children that are not existant; game.Players[math.random(1,#game.Players:GetChildren())] is searching for a child with the random number you have set up, NOT looking for the actual Player/Child.

Another problem is; Why is GetChildren being called on Players? The issue I have with this is; What if there was an Instance-type that IS NOT a Player? And I attempted to call 'Character' on it? Personally, I prefer to check if the Instance-type is a Player, or, to use the GetPlayers [I can not provide the WIKI as it says the page has been terminated] method instead.

Also, Lines 2 and 4 have problems as I have stated earlier; you are again attempting to get Players that are not existant; It is attempting to get Children with the names of Fightera and Fighterb, NOT the random player, and since it's doing that, your code will error as soon as execution starts!

All of this can be fixed, however, but, please review your code over and over [unlike me, where I only check it only a few times], and you may see the Errors pop out at you, also, check the Output as well so that you can detect errors and get a better idea on your problem.

Your code can be fixed, however, as I have stated, please recheck your code a few times before jumping on how your code isn't working, it'll save you time. :)

Now, with all this said, we can fix the code;

local Fightera = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())] --'GetPlayers' creates a table of the current Players within 'game.Players', and will return the table. It is currently choosing a random position in the table of the current Players
Fighter1 = game.Players[Fightera].Name --Use brackets for this to properly get the Player
local Fighterb = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
Fighter2 = game.Players[Fighterb].Name
if Fighter1.Name ~= Fighter1.Name and Fighter2.Name ~= Fighter1.Name then
    Message.Text = (""..Fightera..""..Fighterb..". Thank you for playing.")
    game.Workspace.Fightera.Position = game.Workspace.yy.Position
    game.Workspace.Fighterb.Position = game.Workspace.yy.Position

Allow me to get more in-depth on some things I did not explain;

On lines 2 and 4, you saw I used Brackets, Brackets are a special kind of String in lua, which we can use for many things, as I used on those lines of code; Getting the name without calling it on the child that was not chosen as the random player, and it allows you to put spaces in your strings for the Child.

The GetPlayer method creates a table of the current Children within game.Players, and will return the table.

The Output is a box that will display different things; Errors, Code, orStrings that have been printed using the Print function [Won't print for errors or code I don't think :P].

Hope this helped!

Answer this question