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

Trying to get children of player which are called jar?

So you step on a part then you get the tool "Jar" added to your inventory. The limit of jars supposed to be 5 and stop giving you jars when that happens. So I create a table by getting the humanoids children named "Jar". The table is instantly at 27 when I step on the part so that is what my problem definitely is, just I do not know how to fix it. If any idea's, comment and if you provide a solution that works I will accept your answer and add to your reputation.

local object = script.Parent

local debounce = false

function givePlayerJar(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		if debounce == false then
			debounce = true
			local jarCheck = hit.Parent:GetChildren("Jar")
			local jarNum = table.getn(jarCheck)
			print(jarNum)
			if jarNum ~= 6 then
				local cloneJar = game.ReplicatedStorage.Jar:Clone()
				cloneJar.Parent = hit.Parent
				wait(1)
				debounce = false
			end
		end
	end
end

object.Touched:Connect(givePlayerJar)

1 comment

0

local jarCheck = hit.Parent:GetChildren("Jar")

1 answer

✓ Accepted answer

its because this line gets all children of the player, all parts, animation, humanoid etc

local jarCheck = hit.Parent:GetChildren("Jar")

you have to check if the what getchildren returns really is jar because it ignores ("jar")
but you might need to do something else than adding to the varible as i do here

local object = script.Parent
local jar = 0
local debounce = false

function givePlayerJar(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		if debounce == false then
			debounce = true
			local jarCheck = hit.Parent:GetChildren("Jar")
			for i = 1, #jarCheck do
			print(jarCheck[i]) 
				if jarCheck[i].Name == "Jar" then
					jar = jar + 1
				end
			end
				
			
			
			if jar <= 6 then
				local cloneJar = game.ReplicatedStorage.Jar:Clone()
				cloneJar.Parent = hit.Parent
				wait(1)
				debounce = false
			end
		end
	end
end

object.Touched:Connect(givePlayerJar)

1 comment

0

This code does look like it works. I won’t be able to test it til later today but if you don’t mind, I’m going to send you a friends request on roblox. If you would like we can work on some stuff together.

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