Problem with Collector

So I’m trying to make a game where you collect babies and sell them for a profit but in a tycoon style. The babies are automatically stored in a folder in replicated storage and whenever the player touches the part, I want all the babies to get destroyed and the amount of babies to be added to the leaderstat. However, the babies do not get deleted and so the leaderstat would stack.

part.Touched:Connect(function(hit)
	local babieschildren = babies:GetChildren()
	local char = hit.Parent
	local player = Players:GetPlayerFromCharacter(char)
	
	player:FindFirstChild("leaderstats").Babies.Value += #babieschildren
	for _, object in ipairs(babieschildren) do -- loop through the children
		if table.find(baby, object.Name) then
			object:Destroy()
		end
	end
end)

This is my script.

There are a few simple issues with this code:

  1. You never guarantee that your char is actually a player’s Character
  2. You go directly into modifying an instance of the player without an if-statement or conditional (player can be nil if the char isn’t a real character)
  3. It looks like you’re adding to the Babies leaderstat with the total collected count rather than an individual player’s

What is the value of the baby table? Presumably it contains an array of valid baby instance names, but I’d like to be sure.

1 Like

Im sorry but I didnt really understand your request, Im not really familiar with tables since I copied the table from an existing devforum page.

Implementing @company113’s reply into your code:

part.Touched:Connect(function(hit)
	local babieschildren = babies:GetChildren()
	local char = hit.Parent
	local player = Players:GetPlayerFromCharacter(char)

if player then -- since char can sometimes not be a real character, we check if a player is found
local babiesfound = 0 -- count of how many babies were destroyed
	for _, object in ipairs(babieschildren) do -- loop through the children
if table.find(baby, object.Name) then
			object:Destroy()
babiesfound += 1
end
	end
player:FindFirstChild("leaderstats").Babies.Value += babiesfound -- add the amount of babies found
end
end)

Can you show the whole code? We need to know what baby is

1 Like

Got it, so this is my whole code

-- Services
local RepStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Parts
local part = script.Parent
local baby = {"Baby"}
-- Others
local babies = RepStorage:FindFirstChild("Babies")
local isPlayer = nil

part.Touched:Connect(function(hit)
	local babieschildren = babies:GetChildren()
	local char = hit.Parent
	local player = Players:GetPlayerFromCharacter(char)
	
	player:FindFirstChild("leaderstats").Babies.Value += #babieschildren
	for _, object in ipairs(babieschildren) do -- loop through the children
		if table.find(baby, object.Name) then
			object:Destroy()
		end
	end
end)

did u see my code or no? just need to know

Sorry for the late response, it seems like table.find() just checks if the object’s name is “Baby” (not really significant)
Have you tried what I sent above? Tell me if it doesn’t work (and did it give an error in output)

Whenever I touch the part, it takes me back to the script and the last end) of the script has this next to it:
image
I don’t know what this even is.

Hello? Did you see my message?

I regret to say this but I don’t know what that means either, and the code I gave seems normal