Text string errors when it gets set to a player's name plus text

I an coding a lobby system and I want there to be text saying who created (joined first)
the lobby. When I try to set the text of owner to the player then the text “'s Lobby” then I get one of 2 errors depending on if I use player or player.Name. When I use player then i get the error “attempt to concatenate nil with string” but if I use player.Name then I get the error “attempt to index nil with Name.”

Here is my code:

local sgui = script.Parent.SurfaceGui
local owner = sgui.Owner
local requirement = sgui.Requirement
local status = sgui.Status
local publicity = sgui.Publicity

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and player.Character and hit ~= player.Character.PrimaryPart then return end
	if #plyrs == 0 then
		owner.Text = player .. "'s Lobby"
	else
		local idx = table.find(plyrs, player)
		if idx == nil then
			table.insert(plyrs, player)
		end
	end
end)
1 Like

It means that there isn’t a player.

I believe your return statement has a logic error.
Try replacing

if player and player.Character and hit ~= player.Character.PrimaryPart then return end

with

if not (player and player.Character) or hit ~= player.Character.PrimaryPart then return end
1 Like

When I try that, the same error happens at the same point.

1 Like

Error or errors?
I assume it’s a single error now. On line 11, replace player with player.Name

1 Like

I talked about that in the post. When I try to replace player with player.Name I get the error “attempt to index nil with Name.”

1 Like

Yes, but I made a change that should work about resolving that.

1 Like

When I try both your suggestions at the same time, it doesn’t error but noting is added to the table.

1 Like

Then I suspect this part of your check if causing the issue. This would mean that the player’s humanoid would need to touch the part, and not anything else.

Try replacing

if not (player and player.Character) or hit ~= player.Character.PrimaryPart then return end

with

if not (player and player.Character) then return end

to remove the check

1 Like

Now when I run the script, the player’s name is put into the text but the play is not put into the table.

1 Like

The player isn’t added to the table because you never added the code which adds them.
After the owner.Text line, trying adding:

table.insert(plyrs, player)
1 Like

Oh, I didn’t realize that. I must have accidentally deleted it. Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.