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

How to find my player in game.Players within my script?

Asked by 4 years ago

I have a coin on which if you touch it, it should update a number value inside of my player, then destroy itself. But whenever the coin is touched by the player, it keeps saying "player is not a valid member of Players", I've tried printing the player and it says "jediplocoon", so why isn't this recognizing my username as a player? I'm really confused and if you could just point me in the right direction that would be great! Here's my code:

function OnTouched(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local player = hit.Parent
        local Value = game.Players.player.PlayerGui.CoinGui.numcoins.Coins.Value
        local num = math.random(1,3)
        Value = Value + num
        script.Parent:Destroy()
    end
end
script.Parent.Touched:Connect(OnTouched)

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

By using .Player, you’re trying to ascend into the Players container, to verify two things: Is there a Player Object called "Player", or is there a property called "Player"? Both of which effectively are false which is raising your nil exception. To actively filter for something as an Object, you have to concatenate it in via square braces.

local Player = Hit.Parent
game.Players[Player]

However, will not work. Hit.Parent theoretically references the Character Object if a Player touches it, meaning we’ll actually be trying to perform game.Players[Model] which can make sense why it will fail. The Player Object’s Name is identical to the Handle of their Character Rig, so you can solve this by writing Hit.Parent.Name.

local Player = Hit.Parent.Name
game.Players[Player] --// Reads as game.Players.Feahren if I touch.

Though this does solve the issue, it can still cause problems. Anything can be placed into the Players container which can be named Feahren meaning it could potentially try to access something other than a Player Object, to be more efficient, and ensure this won’t happen, we can use the :GetPlayerFromCharacter method to pair the Player Object from Hit.Parent to be sure we’re always getting a Player.

local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

Be sure to call this below your conditional that filters for a Humanoid Instance. This way we can be almost positive we’re dealing with a Character.

0
You also could use game.Players.LocalPlayer for localscripts. Geobloxia 251 — 4y
0
You cannot run LocalScripts in workspace. Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hi, so in line 5, your saying that in Players you want to find player, which is your variable hit.parent. You need to use local player, so that you can find the local player. hit.parent, would try and find the parent of whatever touched it, which is not in players. Your character is in the workspace, while the player stores your inventory, scripts, and other items. Try this

function OnTouched(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local player = hit.Parent
        local Value = game.Players.LocalPlayer.PlayerGui.CoinGui.numcoins.Coins.Value -- Find the local player, in players, and add the money.
        local num = math.random(1,3)
        Value = Value + num
        script.Parent:Destroy()
    end
end
script.Parent.Touched:Connect(OnTouched)
0
Tell me if it works, if it doesn't im not sure about adding the math.random thing. If it doesn't then you might have to add it after. zandefear4 90 — 4y

Answer this question