How do I make this correctly give me the information I want? It’s meant to update the response and return it, however it always returns the “NotLoaded”
function obtaintool(plr)
local response = "NotLoaded"
local asgv = plr.Character:GetChildren()
for i=1,#asgv do
for i=1, #gunnames do
--print(gunnames[i])
if asgv[i].Name == tostring(gunnames[i]) then
if asgv[i]:IsA("Tool") then
response = tostring(gunnames[i])
end
end
end
end
return response
end
You should debug your code first. You only have one print but you should consider printing more than just that one time. The following cases are good:
The current index of each for loop
After you set the response variable within the for loop
Before you return the response variable
There are some ways you can improve this altogether though…
You can return in the for loop itself and the result will propagate upward instead of setting the response variable – this is also better to do so your code doesn’t waste time iterating additional items if it obtains a valid match and instead returns immediately when there’s a match.
Going for an even better improvement, you should look into CollectionService or attributes. There shouldn’t be a reason why you need to do nested loops where you’re checking every element of a table for every child of a character. You should instead be checking what’s living in the instance’s data and making a general categorisation for applicable tools rather than relying on names.
You could simplify your code to a simple check using either of the above options:
local function obtainTool(player)
if not player.Character then return "NotLoaded" end
local tool = player.Character:FindFirstChildWhichIsA("Tool")
if tool and tool:GetAttribute("Gun") == true then
--if tool and CollectionService:HasTag(tool, "Gun") then -- Alternatively
return tool -- Or it's name since you return a tostring
end
return "NotLoaded"
end
It still returns “NotLoaded”, even if I hold out my tool.
This is the part where it’s called on. If I remove the check for NotLoaded, it tells me its NotLoaded. if the check is there, nothing happens.
task.spawn(function()
local findgun = obtaintool(plr)
if findgun ~= "NotLoaded" then
print(findgun)
local Ranges = returndmgstat(findgun)
local DamageStat = returnrangestat(findgun)
print(Ranges)
print(DamageStat)
-- this is cut off since it doesnt get used, this is the part where it tries to obtain the gun
end)
If you’re going to copy and paste my code and then immediately tell me it doesn’t work, the least you can do is debug it or make a bit of an effort to understand either the code or my explanation and try a bit of problem resolution yourself. It’s not going to work out of the box, it still expects something to meet the conditional in the first place - the attribute.
I don’t post code for it to be copy-pasted. I post it to serve as a learning resource and for the developer receiving it to fill in the blanks or adapt it in their own way.
local function obtaintool(player)
if not player.Character then return "NotLoaded" end
local tool = player.Character:FindFirstChildWhichIsA("Tool")
if tool and tool:GetAttribute("Gun") == true then
--if tool and CollectionService:HasTag(tool, "Gun") then -- Alternatively
print("gunfound")
return tool.Name -- Or it's name since you return a tostring
end
return "NotLoaded"
end