local player = script.Parent.Parent.Parent.Parent.Parent.Parent
Cost = 5
function checkForChocolateMilk()
local found = false
for i, v in pairs(player.Backpack:GetChildren()) do
if v.Name == "ChocolateMilk"
then
found = true
end
end
for i, v in pairs(player.Character:GetChildren()) do
if v.Name == "ChocolateMilk"
then
found = true
end
end
if found == true
then
return true
else
return false
end
end
script.Parent.MouseButton1Click:Connect(function()
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats
then
local Points = leaderstats:FindFirstChild("Points")
end
local Points = leaderstats:FindFirstChild("Points")
if Points then
local hasChocolateMilk = checkForChocolateMilk()()
if Points.Value >= Cost and hasChocolateMilk == false then
Points.Value = Points.Value - Cost
local ChocolateMilk = game.ServerStorage.ChocolateMilk:Clone()
ChocolateMilk.Parent = player.Backpack
end
local found = false
for i, v in pairs(player.Backpack:GetChildren()) do
if v.Name == "ChocolateMilk"
then
found = true
end
if found == true
then
script.Parent.Parent.Parent.ChocolateMilkOwned.Visible = true
script.Parent.Roundify.ImageColor3 = Color3.fromRGB(255, 44, 47)
end
end
end
end)
✓ ANSWER ACCEPTED
why is there and error on line 33 something to do with "FindFirstChild"?
2 answers
✓ Accepted answer
Seems like you're trying to only set Points if leaderstats exists, which is not what your code is currently doing. Looks like you have some confusion in general about if blocks. Change the MouseButton function to something like this:
script.Parent.MouseButton1Click:Connect(function()
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local Points = leaderstats:FindFirstChild("Points")
if Points then
local hasChocolateMilk = checkForChocolateMilk()
if Points.Value >= Cost and hasChocolateMilk == false then
Points.Value = Points.Value - Cost
local ChocolateMilk = game.ServerStorage.ChocolateMilk:Clone()
ChocolateMilk.Parent = player.Backpack
end
local found = false
for i, v in pairs(player.Backpack:GetChildren()) do
if v.Name == "ChocolateMilk" then
found = true
break
end
end
if found == true then
script.Parent.Parent.Parent.ChocolateMilkOwned.Visible = true
script.Parent.Roundify.ImageColor3 = Color3.fromRGB(255, 44, 47)
end
end
end
end)
You also should break out of the for look when what you want is found, as seen above
1 comment
0
i know it says its the answer but it isnt it doesnt work for some reason
In your code, you check if leaderstats has been created and then create the Points IntValue, however directly after it you check for points outside of the statement, therefore nullifying the purpose of the if statement to catch the potential error.
tl;dr: remove line 33
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
whats the error?
Players.diggerbear1.PlayerGui.Store.MainShop.Frame.Frame.BuyChocolateMilk.Script:33: attempt to index nil with 'FindFirstChild'