Player PlatformStand on join

Hello, i am working on a game where when you spawn, you PlatformStand, and im trying to figure out why the script isnt working, and possibly how to fix it.

game.Players.PlayerAdded:Connect(function(plr)
plr.Character.Humanoid.PlatformStand = true
end)

(error: [ServerScriptService.Script:2: attempt to index field ‘Character’ (a nil value)])

Thanks.

What is the error? If there is none, how do you know it isn’t working? I’m also going to assume you’re doing this from a server script.

oh yea, its a server script and forgot to put it in the post, here it is.

[ServerScriptService.Script:2: attempt to index field ‘Character’ (a nil value)]

add this on the first line of function


repeat wait() until plr.Character
1 Like

The problem is you are trying to access the player’s character right when they join, before the character is loaded.
You should put a Player.CharacterAdded event inside your PlayerAdded event and then set the Humanoids state.

2 Likes

Wait @roowbloxer , are you trying to do this on the first time they spawn or every time? if you only need to do it the first time use my method, if you need to do it every time use axo’s method but remember to disconnect the events to prevent memory leaks

1 Like

Okay, first thing is first: Please don’t do this.
Even if the goal is to only use it once. You can do it without using a repeat loop. Which is totally unneeded when it comes to waiting for the player’s character. Every event is able to utilize the :Wait() function. It’s in the name of the function. It yields the script until the event fires, and is much quicker then using wait, and not as inaccurate.

What AxoLib I believe was trying to state is have the CharacterAdded event placed in the script, but have the script yield until it fires. Not connect a function to it.
Doing such thing is very easy. Simply replacing :Connect() with :Wait() and minus the function!

local character = Player.CharacterAdded:Wait()
3 Likes

This solution will cause the thread to infinitely yield if the character never loads, causing a memory leak.

It’s very important to be able to disconnect character logic when the player leaves by doing something like this:

local playerConnections = {}
local function playerAdded(player)
	assert(not playerConnections[player]) -- Make sure the player isn't already connected

	local characterCn;
	local function characterRemoved()
		if characterCn then
			characterCn()
			characterCn = nil
		end
	end
	local function characterAdding(character)
		characterRemoved()
		print("Connecting to character", character)
		
		characterCn = function()
			print("Disconnecting from character", character)
			
		end
	end
	
	local cn1 = player.CharacterRemoving:Connect(characterRemoved)
	local cn2 = player.CharacterAdded:Connect(characterAdding)

	playerConnections[player] = function()
		cn2:Disconnect()
		cn1:Disconnect()
		characterRemoved()
	end
end
local function playerRemoving(player)
	local cn = playerConnections[player]
	if cn then
		playerConnections[player] = nil
		cn()
	end
end

local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(playerRemoving)
Players.PlayerAdded:Connect(playerAdded)

-- Connect to existing players if the script starts late
for _, player in ipairs(Players:GetPlayers()) do
	-- The playerAdded function should not yield/wait here
	playerAdded(player)
end

Even the snippet on the wiki may cause a memory leak:

See this post:

It’s possible that the player is truly Destroy’d when the player leaves, in which case there is no connection leak in the wiki’s case, but keeping track of connections is still a good design habit.

1 Like