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

Trying to work on a Shop GUI and i keep getting an error from my output How did this happen?

Asked by 6 years ago

Im working on a shop GUI and im just one error away from completing it. it keeps saying "attempt to index upvalue 'tool' (a nil value)". How do I fix it? here's my script attempt :

01local price = script.Parent.Parent.Price
02local tools = game.ReplicatedStorage:WaitForChild("Tools")
03local tool = tools:FindFirstChild(script.Parent.Parent.ItemName.Value)
04local player = game.Players.LocalPlayer
05 
06script.Parent.MouseButton1Click:Connect(function()
07    if player.leaderstats:FindFirstChild("Money").Value >= price.Value then
08        player.leaderstats:FindFirstChild("Money").Value = player.leaderstats:FindFirstChild("Money").Value - price.Value
09        local clone = tool:Clone()
10        clone.Parent = player.Backpack
11        local clone2 = tool:Clone()
12        clone2.Parent = player.StarterGear
13    end
14end)

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The reason why you get the error: Attempt to index upvalue 'tool' (A nil value) is to cause the way to that Tool isn't correct. In this case, I can only short your code and make it easier to understand. I prefer using :WaitForChild() cause it waits for something to be loaded, in your case you should be using it, that may be the reason why the script didn't work. As I see, you used :FindFirstChild() well, you should not be using it cause it searches for the part even before the part is loaded or tool, so you should be using :WaitForChild().

01local ToolPrice = script.Parent.Parent:WaitForChild("Price")
02local Tools = game.ReplicatedStorage:WaitForChild("Tools")
03local ToolName = script.Parent.Parent:WaitForChild("ItemName")
04local Tool = Tools:WaitForChild(ToolName.Value)
05local Player = game:GetService("Players").LocalPlayer
06local leaderstats = Player:WaitForChild("leaderstats")
07local CurrencyType = "Money"
08local Currency = leaderstats:WaitForChild(CurrencyType)
09 
10script.Parent.MouseButton1Click:Connect(function()
11 
12    if Currency.Value >= ToolPrice.Value then -- Checks if player has right amount
13 
14        Currency.Value = Currency.Value - ToolPrice -- Takes Amount of money after purchased
15 
View all 24 lines...
0
WaitForChild isn't always the fix. The objects are already there. User#19524 175 — 6y
0
what do you mean "code"? Kul3dud3123 -5 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Actually why its not working is because its not FE. With latest Roblox update you need to make it FE compatible. And you should use RemoteEvent and RemoteFunctions. If you dont know what is FE, then you should read these pages first.

https://wiki.roblox.com/index.php?title=API:Class/Workspace/FilteringEnabled

https://wiki.roblox.com/index.php?title=API:Class/RemoteEvent

https://wiki.roblox.com/index.php?title=API:Class/RemoteFunction

Answer this question