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

can someone Help me with the "attempt to index local 'Player' (a nil value)" please?

**I am programing a game where you collect trash, exchange for money and then use your money, Im trying to make a pressure plate , when you step on it, you buy the item if you have enough money(like the survive the disasters one) , BUT it isn't working! **


 `function ontouched (part) 
  local RS = game:GetService("ReplicatedStorage")
  local item =  RS:WaitForChild("Sword")
  local price = 100 -- Change Your Price Here
  local Player = game.Players.LocalPlayer
  local stats = Player:WaitForChild("leaderstats")
 
  if stats.Pennys.Value == price then -- Change the Money to your Currency Name
   stats.Pennys.Value = stats.Pennys.Value - price
   local cloned = item:Clone()
   local cloned2 = item:Clone()
   cloned2.Parent = Player.Backpack
   cloned.Parent = Player.StarterGear
  end
 
 end
 	
 -- the player gets an error "attempt to index local 'Player' (a nil value)" please help	
  

  script.Parent.Touched:Connect(ontouched)


`

1 comment

0

You can also just do: item:Clone().Parent = Player.Backpack and item:Clone().Parent = Player.StarterGear. And, if you're not using a localscript, LocalPlayer does not exist.

2 answers

✓ Accepted answer

I assume that you are using normal script. So you can't get player by doing game.Players.LocalPlayer. Solution to this would be using :GetPlayerFromCharacter:

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

, also not every touch is from player so you can check for humanoid:

if part.Parent:FindFirstChild("Humanoid")  then
       Code you have written
end

This if statement would go after function is called

I think the reason for your issue is because you're not using a local script. If you want to use localplayer. Then use a local script:=)

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