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

How do I make the script choose a random number between 1 and 2 when the player is added?

All I want is for when a player is added, and a value inside of them is 0, I want it to pick a random number between 1 and 2 to assign to it, but I cant get it to work.

local Player = game.Players.LocalPlayer 
local Players = game:GetService("Players")

local RandomSkill = math.random(1,2)



function onPlayerAdded(player)
     if Player.Levelss.RandomMove.Value == 0 then
	Player.Levelss.RandomMove.Value = Player.Levelss.RandomMove.Value + RandomSkill
end
end
 

Players.PlayerAdded:connect(onPlayerAdded)

1 comment

0

FYI: i looked at the output, and it gave me nothing

1 answer

✓ Accepted answer

This isn't working for a couple of reasons.

First, you call math.random only once, so the "random" value will be the same for each new player. Secondly, you have the player parameter in your event listener function, yet you use Player, the local player. Judging by your usage of LocalPlayer, I'm assuming this is a local script; it would be preferable if you moved it into a server script.

You can solve these issues by moving the call to math.random and using the player parameter instead of the LocalPlayer:

game:GetService('Players').PlayerAdded:connect(function(player)
	if player:WaitForChild('Levelss'):WaitForChild('RandomMove').Value == 0 then
		player.Levelss.RandomMove.Value = player.Levelss.RandomMove.Value + math.random(1,2);
	end;
end);

Read more about anonymous functions and WaitForChild.

Hope this helped.

1 comment

0

Well explained.

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