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

If Statements and Math.Random Not Working?

So I want to make it so that if you are a certain team, then you will automatically go to one of four spawn points. Here is the script I have for this:

if game.Players.LocalPlayer.TeamColor == "Really Red" then
		KnightSpawnPoint = math.random(4)
	if KnightSpawnPoint == 1 then
		game.Players.LocalPlayer.Character.MoveTo(447, 105.225, 299.22)
	elseif KnightSpawnPoint == 2 then
		game.Players.LocalPlayer.Character.MoveTo(312.7, 105.225, 303.22)
	elseif KnightSpawnPoint == 3 then
		game.Players.LocalPlayer.Character.MoveTo(315.7, 108.713, 432.77)
	elseif game.Players.LocalPlayer.TeamColor == "Really Red" and KnightSpawnPoint == 4 then
		game.Players.LocalPlayer.Character.MoveTo(446.775, 108.713, 432.77)
	end
end

I have this script (not local script) in the workspace and no errors come up, but it wont work? Can you please explain what I did wrong?

2 answers

You can't use LocalPlayer in a server script. If you are doing this for all players, then try looping through everyone and using player as the player instead.

Use this:

for _,player in pairs(game.Players:GetChildren()) do
	-- Put your code here.
end

1 comment

0

So, what do you mean by --Put your code here? Are you saying all the code, because if so that didn't work. Are you talking about some parts of the code? I don't know what to put inside that?

First of all, you must use ":" to use functions, not ".". So, instead of ".MoveTo" use ":MoveTo"

Next, you can not use LocalPlayer in a server script, due to that the server has no players. We will get the players from Players and make a "for in" loop.

If you want the script to run and check continuously, you will need to do a "while true do" (If that is not the case, delete "while true do", "wait()" and one "end". But remember that if so, this script will only run once if you don't connect it to something!

And, too, you should first make clear that "KnightSpawnPoint" is a local variable.

Now, lets fix it.

local KnightSpawnPoint

while true do
wait()
for i,playertbl in pairs(game.Players:GetChildren()) do
    if playertbl[i].TeamColor == "Really Red" then
        KnightSpawnPoint = math.random(1, 4)
    if KnightSpawnPoint == 1 then
        playertbl[i].Character:MoveTo(447, 105.225, 299.22)
    elseif KnightSpawnPoint == 2 then
        playertbl[i].Character:MoveTo(312.7, 105.225, 303.22)
    elseif KnightSpawnPoint == 3 then
        playertbl[i].Character:MoveTo(315.7, 108.713, 432.77)
    elseif game.Players.LocalPlayer.TeamColor == "Really Red" and KnightSpawnPoint == 4 then
        playertbl[i]:MoveTo(446.775, 108.713, 432.77)
    end
end
end

Hope it helps! Tell me if it doesn't work or if you have any doubts.

2 comments

0

Hey, I get this error if I put the script in that says, 1 is not valid member of player? Can you please help?

0

Is KnightSpawnPoint a variable?

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