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

Trying to get children of player which are called jar?

Asked by
M9F 94
4 years ago
Edited 4 years ago

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)
0
local jarCheck = hit.Parent:GetChildren("Jar") Kaptajnfar 15 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
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. M9F 94 — 4y
Ad

Answer this question