I’m working on a script that gives me money in the leaderstats when I click a part and destroys a part when I click it as well. The problem is my script multiples my money, i’m inable to properly use Destroy(), and I have to use a while loop to update it which ruins the whole script. I don’t know how to approach this script because it’s very specific.
while true do
task.wait(1)
for i,v in pairs(workspace.ItemWaitingFolder:GetChildren()) do
v.ClickDetector.MouseClick:Connect(function(plr)
v.Parent = game.ServerStorage.GarbageFolder
local leaderstats = plr:WaitForChild("leaderstats")
local value = leaderstats:WaitForChild("Money")
if v.Name == "Cereal" then
value.Value += 3
elseif v.Name == "Beef" then
value.Value += 2
elseif v.Name == "Cake" then
value.Value += 1
elseif v.Name == "Cupcakes" then
value.Value += 1.5
end
end)
end
end
Garbaging script:
while true do
task.wait(50)
game.ServerStorage.GarbageFolder:ClearAllChildren()
end
Why are you adding connections in a loop? That’s what is multiplying your money.
Get rid of your GarbageFolder and the Garbaging script too.
Code:
--//Variables
local itemWaitingFolder = workspace.ItemWaitingFolder
--//Tables
local itemWorthTable = {
Cereal = 3,
Beef = 2,
Cake = 1,
Cupcakes = 1.5
}
--//Functions
local function InitializeItem(item)
local itemWorth = itemWorthTable[item.Name]
local clickDetector = item.ClickDetector
clickDetector.MouseClick:Once(function(plr)
item:Destroy()
local leaderstats = plr.leaderstats
local money = leaderstats.Money
money.Value += itemWorth
end)
end
itemWaitingFolder.ChildAdded:Connect(InitializeItem)
for i, v in workspace.ItemWaitingFolder:GetChildren() do
task.spawn(InitializeItem, v)
end
Once I clicked all the spawned parts I got “The Parent property of Cereal is locked, current parent: NULL, new parent ItemWaitingFolder” in the output.
Could you explain it better? Do you mean every time you clicked a part it said that? Or do you actually mean “Once you clicked every spawned part” then you got the error? What type of script is this too?
Try this code:
--//Variables
local itemWaitingFolder = workspace.ItemWaitingFolder
--//Tables
local itemWorthTable = {
Cereal = 3,
Beef = 2,
Cake = 1,
Cupcakes = 1.5
}
--//Functions
local function InitializeItem(item)
local itemWorth = itemWorthTable[item.Name]
local clickDetector = item.ClickDetector
clickDetector.MouseClick:Once(function(plr)
local leaderstats = plr.leaderstats
local money = leaderstats.Money
money.Value += itemWorth
item:Destroy()
end)
end
itemWaitingFolder.ChildAdded:Connect(InitializeItem)
for i, v in itemWaitingFolder:GetChildren() do
task.spawn(InitializeItem, v)
end
I clicked all the parts I had in another script which spawns parts that go in a folder, and when I clicked all of them it said that line. They are all clicked, also meaning they are all destroyed.
I wrote this on mobile so i apologize for any typing errors
local folder = workspace.ItemWaitingFolder
local Clickable = folder:GetChildren())
local function Click(part)
if not part:GetAttribute("Clickable") then
part:SetAttribute("Clickable", true)
local con
con = part.FindFirstChild("ClickDetector").MouseClick:Connect(function(plr)
part.Parent = game.ServerStorage.GarbageFolder
local leaderstats = plr:WaitForChild("leaderstats")
local value = leaderstats:WaitForChild("Money")
if part.Name == "Cereal" then
value.Value += 3
elseif part.Name == "Beef" then
value.Value += 2
elseif part.Name == "Cake" then
value.Value += 1
elseif part.Name == "Cupcakes" then
value.Value += 1.5
end
con:Disconnect()
end)
end
end)
for i,v in pairs(Clickable) do
Click(v)
end
folder.ChildAdded:Connect(function(p)
Click(p)
end)
I’m creating new parts in that folder(ItemWaitingFolder). Also i’m transporting parts in the garbagefolder, and I made the garbaging script is to destroy them each 50 seconds.
I was trying to avoid the NULL error but it keeps coming in my script, I put the title as working script but I just realized it’s not actually working.
make sure you add a CS tag named clickable to those objects in folder, it should work now i hope…
local CS = game:GetService("CollectionService")
local Clickable = CS:GetTagged("Clickable")
local function Click(part)
if not part:GetAttribute("Clickable") then
part:SetAttribute("Clickable", true)
local con
con = part.FindFirstChild("ClickDetector").MouseClick:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
local value = leaderstats:WaitForChild("Money")
if part.Name == "Cereal" then
value.Value += 3
elseif part.Name == "Beef" then
value.Value += 2
elseif part.Name == "Cake" then
value.Value += 1
elseif part.Name == "Cupcakes" then
value.Value += 1.5
end
part:Destroy()
con:Disconnect()
end)
end
end
for i,v in pairs(Clickable) do
Click(v)
end
CS:GetInstanceAddedSignal(Click)