so i am trying to check if player have a tool, if not i will have to clone one for the player, but the problem is the tool have changed name, how would i get that name? do i do a ton of ifs? here is the tool name changing script
local hasTool = false
for _, obj in pairs(player.Character:GetChildren()) do
if obj:IsA("Tool") then
hasTool = true
break -- stop the loop once found
end
end
if hasTool == false then
-- create the tool
end
You could loop through the tool using a for loop and get children.
An example of this would be
for i,v in pairs(character:GetChildren()) do
--check if v is the tool here
end
v is a random object inside of the character. The code inside of the loop is ran for every child of the character, with v being the random child the looped code is checking.
To check if âvâ is the tool, and not something random, check if v has properties equal to that of the tool. Does the tool have a brown part? if so, check if âvâ has a brown part. Does the tool have a particle emitter? check if âvâ has a particle emitter. If you need more help for the specific checking part, just reply to my message with an image of the contents of the tool.
In this case, you could do what @XxFishShadowxX said and check for other properties of the tool.
If you want each tool to be individually identifiable, I suppose something Iâd do is put a StringValue or an IntValue inside each tool and have each value be unique. Then, if youâre looking for the exact tool you want, you can check this value and if it corresponds, youâd know it is the one youâre looking for.
Then modify the loop I wrote:
for _, obj in pairs(player.Character:GetChildren()) do
if obj:IsA("Tool") and obj.IntValue.Value == 7 then -- new here
hasTool = true
break -- stop the loop once found
end
end
how would i do something when it is not found? i am trying to make a breaking system like minecraft, which sees if you donât have the tool you get one from rep storage, hereâs what i have so far
wait()
local Stone = script.Parent.Value.Value
a = true
script.Parent.At.Break.Triggered:Connect(function(plr)
if Stone ~= 0 then
if a then
a = false
Stone = Stone - 1
script.Parent.Parent.Thing.Transparency = script.Parent.Parent.Thing.Transparency + 0.25
script.Parent.Parent.Thing2.Transparency = script.Parent.Parent.Thing2.Transparency + 0.25
script.Parent.Parent.Thing3.Transparency = script.Parent.Parent.Thing3.Transparency + 0.25
script.Parent.Parent.Thing4.Transparency = script.Parent.Parent.Thing4.Transparency + 0.25
script.Parent.Parent.Thing5.Transparency = script.Parent.Parent.Thing5.Transparency + 0.25
wait(1)
a = true
end
end
if Stone == 0 then
for _, obj in pairs(plr.Character:GetChildren()) do
if obj:IsA("Tool") and obj.IntValue.Value == 7 then -- new here
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
break -- stop the loop once found
end
end
end
end)
You could perhaps insert a string value inside the tool named âWoodâ and then you could run a for loop to check if the player owns a tool that has the âWoodâ string value inside it with :FindFirstChild()
local hasTool = false
for _, obj in pairs(player.Character:GetChildren()) do
if obj:IsA("Tool") then
hasTool = true
break -- stop the loop once found
end
end
if hasTool == false then
-- create the tool
end
When the tool is not found, you would write the instructions for what happens in that event where I commented â-- create the toolâ at the bottom
hmm so far i got this, do i put the backpack loop in the hastool = false part?
wait()
local Stone = script.Parent.Value.Value
a = true
hasTool = false
script.Parent.At.Break.Triggered:Connect(function(plr)
if Stone ~= 0 then
if a then
a = false
Stone = Stone - 1
script.Parent.Parent.Thing.Transparency = script.Parent.Parent.Thing.Transparency + 0.25
script.Parent.Parent.Thing2.Transparency = script.Parent.Parent.Thing2.Transparency + 0.25
script.Parent.Parent.Thing3.Transparency = script.Parent.Parent.Thing3.Transparency + 0.25
script.Parent.Parent.Thing4.Transparency = script.Parent.Parent.Thing4.Transparency + 0.25
script.Parent.Parent.Thing5.Transparency = script.Parent.Parent.Thing5.Transparency + 0.25
wait(1)
a = true
end
end
if Stone == 0 then
for _, obj in pairs(plr.Character:GetChildren()) do
if obj:IsA("Tool") and obj.StringValue.Value == "Table" then
hasTool = true-- new here
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
break -- stop the loop once found
end
end
if hasTool == false then
local clone = game.ReplicatedStorage.Table:Clone()
clone.Parent = plr.Parent
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
end
end
end)
oh it says ":33:42.767 StringValue is not a valid member of Tool âWorkspace.foxnoobkite.Crafting tableâ "
i think i called itâs class name instead of its name
For that error, check to make sure the StringValue actually exists in the table.
For where to loop through the Backpack, just copy and paste this same loop below it:
for _, obj in pairs(plr.Character:GetChildren()) do
if obj:IsA("Tool") and obj.StringValue.Value == "Table" then
hasTool = true-- new here
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
break -- stop the loop once found
end
end
EXCEPT, loop through the backpack instead and add a new check:
for _, obj in pairs(plr.Backpack:GetChildren()) do -- HERE
if hasTool == true then
break -- stop the loop
end
if obj:IsA("Tool") and obj.StringValue.Value == "Table" then
hasTool = true-- new here
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
break -- stop the loop once found
end
end
hm it seems to work until the second loop, it seems to be giving me error saying that the value is not there for a tool that does not the value, how would i fix that
You can use string.match to detect if the name of the tool includes a word that you are trying to find.
-- For player's backpack
for i, v in pairs(player.Backpack:GetChildren()) do
if string.match(v.Name, "Wood") and v:IsA("Tool") then
-- the player has the Wood tool
end
end
-- For player's character
if player.Character then
for i, v in pairs(player.Character:GetChildren()) do
if string.match(v.Name, "Wood") and v:IsA("Tool") then
-- the player has the Wood tool
end
end
end
i just followed Physicismâs suggestion and modifyed it into my own code.
wait()
local Stone = script.Parent.Value.Value
a = true
hasTool = false
script.Parent.At.Break.Triggered:Connect(function(plr)
if Stone ~= 0 then
if a then
a = false
Stone = Stone - 1
script.Parent.Parent.Thing.Transparency = script.Parent.Parent.Thing.Transparency + 0.25
script.Parent.Parent.Thing2.Transparency = script.Parent.Parent.Thing2.Transparency + 0.25
script.Parent.Parent.Thing3.Transparency = script.Parent.Parent.Thing3.Transparency + 0.25
script.Parent.Parent.Thing4.Transparency = script.Parent.Parent.Thing4.Transparency + 0.25
script.Parent.Parent.Thing5.Transparency = script.Parent.Parent.Thing5.Transparency + 0.25
wait(1)
a = true
end
end
if Stone == 0 then
for _, obj in pairs(plr.Character:GetChildren()) do
if obj:IsA("Tool") and obj.Value.Value == "Table" then
hasTool = true-- new here
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
break -- stop the loop once found
end
end
for _, obj in pairs(plr.Backpack:GetChildren()) do -- HERE
if hasTool == true then
break -- stop the loop
end
if obj:IsA("Tool") and obj.Value.Value == "Table" then
hasTool = true-- new here
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
break -- stop the loop once found
end
end
if hasTool == false then
local clone = game.ReplicatedStorage:WaitForChild("Crafting table"):Clone()
clone.Parent = plr.Backpack
plr.Resources.Table.Value = plr.Resources.Table.Value + 1
script.Parent.At.Break:Destroy()
script.Parent.At.Table:Destroy()
script.Parent.Parent:Destroy()
end
end
end)