Still have questions? Join our Discord server and get real time help.
0

# Problem with tool parenting and finding child in the table. Any help?

Edited 2 days ago

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)

0
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. Vathriel 477 — 2d
0
one more thing, you don't need to use playerTable[i].Name, you can use v.Name in this case. Vathriel 477 — 2d
0
if you put it to 5 then it wont count the first one and give you 6 IckyTheSuperior 25 — 2d
0
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. Vathriel 477 — 2d
0
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. IckyTheSuperior 25 — 2d
0
Would you like to come into a studio with me and further check it out? IckyTheSuperior 25 — 2d
0
Of course the print shows 0 the first time it's printed. In order a tool isn't added until after the print. Vathriel 477 — 2d
0
if I make it where the function that clones the part then print, it still doesn't work. IckyTheSuperior 25 — 2d
0
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. Vathriel 477 — 2d
0
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? IckyTheSuperior 25 — 2d

0
Vathriel 477
2 days ago
Edited 2 days ago
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 this is the first click we're not going to see anything because there isn't anything to see.
if playerTable[i].Name == "Tool" then
amountOfTool = amountOfTool + 1
end
end
while amountOfTool < 5 do --No matter which click this is refill to 5
local clonedTool = objectTool:Clone()
clonedTool.Parent = plr
amountOfTool = amountOfTool + 1
end
wait(1)
debounce = false
end
end
end

object.Touched:Connect(givePlayerItem)


Your problem is that you were doing this:

Touch > Does the player have tools (on first touch this is obviously 0) > add a tool if we have less than 7 tools > Exit the function

When the logic you're looking for is:

Touch > Does the player have tools? (on first touch this is obviously 0) > Add tools until the player has 5 > Exit the function

You could also try:

Touch > Does the player have tools? (on first touch this is obviously 0) > Add a tool > Exit > Does the player have tools? > Add a tool > Exit > etc..

player = game.Players:GetPlayerFromCharacter(plr)
for i,v in pairs(player.Backpack:GetChildren()) do
table.insert(playerTable,v)
end


where plr is the character object you had previously, and playerTable is the table you had previously.

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 this is the first click we're not going to see anything because there isn't anything to see.
if playerTable[i].Name == "Tool" then
amountOfTool = amountOfTool + 1
end
end
if amountOfTool < 5 do
local clonedTool = objectTool:Clone()
clonedTool.Parent = plr
end
wait(1)
debounce = false
end
end
end

object.Touched:Connect(givePlayerItem)

0
is there any way to add up to five not instantly but every time you touch the block after the debounce is done IckyTheSuperior 25 — 2d