I get this error:

Workspace.SellPlatform.SellScript:29: attempt to index a nil value

For this script:

-- When a player touches the parent object, their items will be sold for gold
local sellPart = script.Parent
 
-- Gives the player gold for each item they have
local function sellItems(playerItems, playerGold)
	-- Gives players 100 pieces of gold for each item
	local totalSell = (playerItems.Value * 100)
	playerGold.Value = playerGold.Value + totalSell
	playerItems.Value = 0
end
 
local function onTouch(partTouched)
	-- Looks for a Humanoid
	local character = partTouched.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
 
	-- If a humanoid is found, gets leaderstats and calls sellItems function
	if humanoid then
		-- Get the player, so changes can be made to  the player's leaderstats		
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		local playerStats = player:FindFirstChild("leaderstats")
		local playerSpaces = playerStats:FindFirstChild("Spaces")
		local playerItems = playerStats:FindFirstChild("Items")
		local playerGold = playerStats:FindFirstChild("Gold")
		local playerStats2 = player:FindFirstChild("Stats")
		local playerSold = playerStats2:FindFirstChild("Sold")
		local playerSoldRequired = playerStats2:FindFirstChild("SoldRequired")	
		local playerLevel = playerStats2:FindFirstChild("Level")
		local playerLevelGui = player.PlayerGui:FindFirstChild("Stats").Level			
		
		-- Sells Items and then changes the player's spaces and money
		playerSold.Value = playerSold.Value + playerItems.Value
		sellItems(playerItems, playerGold)
			if playerSold.Value >= playerSoldRequired.Value and playerSpaces.Value >= playerItems.Value then
				playerLevel.Value = playerLevel.Value + 1
				playerLevelGui.Visible = true
				playerSoldRequired.Value = playerLevel.Value * 100 + playerSoldRequired.Value
				playerSold.Value = 0
			end
			
			
	end
end
 
sellPart.Touched:Connect(onTouch)

28 comments

0

Yeah, It appears in PlayerGui, but idk...how can i move them there using a script?

0

Yes..But this "index nil value" kills me..

0

Stats is not a valid member of PlayerGui

0

Stats is not a valid member of PlayerGui. I think using StarterGui instead makes it visible to everyone, and i only want the localplayer to see the Gui when he meets the requirement.

0

Ignore the "Stats is not a valid member of PlayerGui." from above.

0

AradIsHere, please stop spreading misinformation.

0

This is the starterGui : http://prntscr.com/ml3nke

0

The frame doesn't turn visible... I tried with "local playerLevelGui = game.StarterGui.Stats.Level" and then "playerLevelGui.Visible = true"

0

It should not have worked for you. That code is just modifying what replicates to clients when they die or join. Now stop spreading misinformation like I asked, AradIsHere.

0

thats why i dont post answers...

0

Why'd you remove your comments? Even if they were wrong, they were important for context.

0

ady1111, I'll help you out. Is the line the error occurs on the same line as line twenty-nine in the script you posted? If not, please tell me the line that is erroring.

0

Yes, line 29 is the problem

0

Ok, for debugging purposes, I'd like you to, before the line that's erroring, write: print(player); print(player.PlayerGui); print(player.PlayerGui:FindFirstChild("Stats"))

0

Tell me if one of the above prints nil.

0

1.ady1111 2.PlayerGui 3.nil

0

Ok, FindFirstChild returns nil when an item with the name matching the string passed as the argument is not found.

0

This is the PlayerGui: http://prntscr.com/ml41dx

0

Right, and that confuses me because this should only be erroring if FindFirstChild returns nil, but FindFirstChild shouldn't be returning nil. Do you mind commenting out that line and telling me what happens?

0

What do you mean?

0

Basically, what happens when you remove that line of code?

0

Workspace.SellPlatform.SellScript:36: attempt to index global 'playerLevelGui' (a nil value)

0

Ok, now we know for sure that the FindFirstChild returning nil is causing the issue.

0

I did some research, and, apparently, the server can't access things in the PlayerGui. You're going to have to modify that gui locally.

0

Remote events are your only option in this scenario.

0

So how should i do it? If the line 34 condition is met, the gui becomes visible...don't know how...I always have trouble with this error.

0

Managed to do it with Remote Events, thank you!

0

Items in StarterGui are cloned locally, so you'll have to use remotes to be able to accomplish what you're trying to do.