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

Why wont this code make one person player 1, and another player 2 in the leaderboard?

I'm making a 2 player game where I want the first player to join to become player 1 on the leaderboards, and the second to become player 2

it selects player 1 just fine, but it wont even give the second player any stats.

local player1
local player2

function Onplayerentered(player)
	player1:Disconnect()
	print("P1 Chosen")
	local leaderstats = Instance.new("IntValue")
	leaderstats.Parent = player
	leaderstats.Value = 0
	leaderstats.Name = "leaderstats"
	
	local P1 = Instance.new("IntValue")
	P1.Parent = leaderstats
	P1.Name = "Player 1" -- Put name here
	P1.Value = 1 -- Put the starting Value#
	
	local P2 = Instance.new("IntValue")
	P2.Parent = leaderstats
	P2.Name = "Player 2" -- Put name here
	P2.Value = 0 -- Put the starting Value#
end

function Onplayerentered2(player)
	player2:Disconnect()
	print("P2 Chosen")
	local leaderstats = Instance.new("IntValue")
	leaderstats.Parent = player
	leaderstats.Value = 0
	leaderstats.Name = "leaderstats"
	
	local P1 = Instance.new("IntValue")
	P1.Parent = leaderstats
	P1.Name = "Player 1" -- Put name here
	P1.Value = 0 -- Put the starting Value#	
	
	local P2 = Instance.new("IntValue")
	P2.Parent = leaderstats
	P2.Name = "Player 2" -- Put name here
	P2.Value = 1 -- Put the starting Value#
end

player1 = game.Players.ChildAdded:Connect(Onplayerentered)

if player1.Connected == false then
	player2 = game.Players.ChildAdded:Connect(Onplayerentered2)
end

2 answers

There is no .Connected property. Don't make stuff up and expect it to work. If it's not in the documentation, it's not there.

At line 43, player1 will be listening because you just connected it.

if means "if", not "when". You accomplish "whens" in ROBLOX using event connections.

Of course, having multiple connections to a single event is silly. You don't need to reconnect and disconnect. Use an if inside.

local first = nil -- the first player
local second = nil -- the second player

function newPlayer(player)
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	local p1 = Instance.new("BoolValue", player)
	p1.Name = "Player 1"
	local p2 = Instance.new("BoolValue", player)
	p2.Name = "Player 2"
	if first == nil then
		first = player
		p1.Value = true
		-- do whatever
	else
		second = player
		p2.Value = true
		-- do whatever
	end
end

game.Players.PlayedAdded:Connect(newPlayer)

You really should use BoolValue not IntValue to store a bool (a one or a zero).

You should use PlayerAdded not ChildAdded.

Realize that you're assuming that only two players will ever join a server and that neither will leave. These are all possible courses of events that break your script:

  • Player A joins
  • Player A quits
  • Player A joins

or

  • Player A joins
  • Player A quits
  • Player B joins
  • Player A joins

etc.

I think .Connected will be nil and not false since you disconnected the connection so it doesn't exist anymore.

Simple fix would be

if player1 then

1 comment

0

If I do just if player1 then, it chooses both P1 and P2 when just 1 player joins, but when I do if player1.Connected == nil then it has the same problem as before..

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