The script is supposed to give players only five Tools from replicated storage when a part is touched by a player. The problem is the clone gets added to the Player but the for statement doesn't see it in the table the first time. After the first time the function is fired, then the for statement will see it in the table and add to the amountOfTool variable. Any way to fix this?
local object = script.Parent
local objectTool = game.ReplicatedStorage.Tool
local debounce = false
function givePlayerItem(hit)
if hit.Parent:FindFirstChild("Humanoid")then
if debounce == false then
debounce = true
local playerTable = hit.Parent:GetChildren()
local amountOfTool = 0
local plr = hit.Parent
for i, v in pairs(playerTable) do
if playerTable[i].Name == "Tool" then
amountOfTool = amountOfTool + 1
print(amountOfTool)
end
end
if amountOfTool <= 6 then
local clonedTool = objectTool:Clone()
clonedTool.Parent = plr
end
wait(1)
debounce = false
end
end
end
object.Touched:Connect(givePlayerItem)

10 comments
I'm not sure I fully understand your meaning- based on what I can see on the first fire the tool would only be added after the for loop- specifically starting at line 20. If your goal is to give players only five though amountOfTool<=6 is wrong as it will then add up to 7 tools total. <5 is what you should be using.
one more thing, you don't need to use playerTable[i].Name, you can use v.Name in this case.
if you put it to 5 then it wont count the first one and give you 6
Based on your stated problem it will show up by the time the function is called a second time, so it doesn't really matter in the very first case.
that's the thing, the tool will get added to your player but the variable responsible for setting how many tools you have wont + 1 to itself the first time the function is fired. The print shows it as 0 the first time it is printed. so what is responsible for adding to the variable, it is the for loop. So something is going on with the for loop and how the clone is created.
Would you like to come into a studio with me and further check it out?
Of course the print shows 0 the first time it's printed. In order a tool isn't added until after the print.
if I make it where the function that clones the part then print, it still doesn't work.
your code is mildly out of order, I think I have an idea of what you're trying to do now but it won't be for another hour or so till I can write a full response.
Alright, that's fine. I appreciate it and if you could include what the changes you made would be great! ...Oh yeah, also how would one go by checking the backpack and put that in the same table that the players character is in?