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

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

Asked by 4 years ago

**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)

`

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. Syclya 224 — 4y

2 answers

Log in to vote
0
Answered by
karlo_tr10 1233 Moderation Voter
4 years ago
Edited 4 years ago

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

Ad
Log in to vote
0
Answered by 4 years ago

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:=)

Answer this question