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

Shop Buy Button Error: "attempt to index upvalue 'tool' (a nil value)" ?

Hello, I'm following a tutorial (https://www.youtube.com/watch?v=c8bWOSU7WKw) and I'm struggling in scripting the Buy button thing.

Players.SharkDoodlebanger.PlayerGui.Shop.Frame.Info.Frame.Buy.LocalScript:9: attempt to index upvalue 'tool' (a nil value) 21:07:25.191 - Stack Begin 21:07:25.192 - Script 'Players.SharkDoodlebanger.PlayerGui.Shop.Frame.Info.Frame.Buy.LocalScript', Line 9 21:07:25.192 - Stack End

here is my code:

local price = script.Parent.Parent.Parent.Price local tools = game.ReplicatedStorage:WaitForChild("Tools") local tool = tools:FindFirstChild(script.Parent.Parent.Parent.ItemName.Value) local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function() if player.leaderstats:FindFirstChild("Token").Value >= price.Value then player.leaderstats:FindFirstChild("Token").Value = player.leaderstats:FindFirstChild("Token").Value - price.Value local clone2 = tool:Clone() clone2.Parent = player.StarterGear end end)

3 comments

0

Smh

0

guess what it means script.Parent.Parent.Parent.ItemName.Value is not a child of tools and returning nil

0

lool

1 answer

✓ Accepted answer
local price = script.Parent.Parent.Parent.Price
local tools = game.ReplicatedStorage:WaitForChild("Tools")
local tool = tools:FindFirstChild(script.Parent.Parent.Parent.ItemName.Value)
local player = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	local item = player.leaderstats:FindFirstChild("Token")
	if item.Value >= price.Value then
		item.Value = item.Value - price.Value
		
		if tool == nil then
			print(script.Parent.Parent.Parent.Name)
		else
			local clone = tool:Clone()
			clone.Parent = player.Backpack

			local clone2 = tool:Clone()
			clone2.Parent = player.StarterGear
		end

	end
end)

The above version of your script will help you determine what is wrong with the parenting

Since "tool" is being returned as nil, make sure that the parenting of

game --> ReplicatedStorage --> Tools --> (script.Parent.Parent.Parent.ItemName.Value)

AND

script <-- Parent <-- Parent <-- Parent --> ItemName

are both correct (you have three parents, the video example has only two written)

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