Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by 6 years ago
Edited 6 years ago
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

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

0
Are you that desperate for reputation lol. User#19524 175 — 6y
0
... WHAT? Answering a person's question is making you desperate for reputation now? Thundermaker300 554 — 6y
0
lol you edited the part that said 'if this answered your question please mark it as answered and upvote it!' User#19524 175 — 6y
1
What's wrong with saying that? Thundermaker300 554 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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)
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. oreoollie 649 — 6y
0
I am spreading absolutely 0 false information. I said ROBLOX *MAY* remove connect. Not they will. Go get a dictionary! User#19524 175 — 6y
0
If there is anyone spreading false information, it’s you. Have a nice day! User#19524 175 — 6y
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. TheeDeathCaster 2368 — 2y

Answer this question