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)
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.