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

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

Asked by
soutpansa 120
7 years ago

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)
0
FYI: i looked at the output, and it gave me nothing soutpansa 120 — 7y

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

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.

0
Well explained. LightModed 81 — 7y
Ad

Answer this question