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

Getting this error: player is not a valid member of Players Not sure why?

local function onTouched(player) local level = game.Players.player.leaderstats.Level

level.Value = level.Value + 1 end

script.Parent.Touched:connect(onTouched)

Line 1 is giving me the error player is not a valid member of Players.

I've tried look this up and didn't find anything, thanks to anyone who helps out! :)

2 answers

✓ Accepted answer

That's because you are trying to find an object named 'player' inside the Players service, which doesn't exist. Additionally, the returned object isn't a player, it's the part that touched. Since the part that touched it can be a limb of a player, we can use the method GetPlayerFromCharacter checking if the parent of the part (the potential character model) has a player.

(On a smaller note, connect is deprecated. Please use Connect.)

function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		-- Found the player!  Do your logic here.
	end
end

script.Parent.Touched:Connect(onTouched)

Also try to fix that formatting up a little bit. Your code needs to be neat and organized, otherwise you won't understand it.

4 comments

0

Are you that desperate for reputation lol.

0

... WHAT? Answering a person's question is making you desperate for reputation now?

0

lol you edited the part that said 'if this answered your question please mark it as answered and upvote it!'

1

What's wrong with saying that?

This is because the BasePart.Touched event returns the BasePart that touched the part. It could be a limb, torso, head, or a part that doesn’t belong to a player. Because of this, you must use the GetPlayerFromCharacter method of the Players service. You should also use Connect since ROBLOX may remove connect soon.

local plrs = game:GetService"Players"

local function onTouch(part) -- part is our parameter
    if part.Parent:FindFirstChild"Humanoid" then -- if we find a humanoid 
        local plr = plrs:GetPlayerFromCharacter(part.Parent) -- self explanatory 
        
        if plr then -- make sure it’s a player, not a part that fell from the sky or something
            local level = plr.leaderstats.Level
            level.Value = level.Value + 1
        end
    end

end

script.Parent.Touched:Connect(onTouch)

4 comments

0

I would just like to inform the OP that part of this information is incorrect. Roblox will not be removing :connect(). It is true that you should use :Connect() because it is not deprecated. @incapaz As stated before, please stop spreading false information about deprecated code.

0

I am spreading absolutely 0 false information. I said ROBLOX MAY remove connect. Not they will. Go get a dictionary!

0

If there is anyone spreading false information, it’s you. Have a nice day!

0

Welp, it's been three or four years now. It won't ever be removed due to widespread use and old scripts relying on it lol.

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