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

How do I check if someone has the tool or not?

Asked by 5 years ago
Edited 5 years ago

I've been making a infection tool that if Q is pressed and the player's head hits the other player, it will remove all of their tools and give them the infection tool. My issue is that I tried seeing if the tool in the player's inventory is equal to "nil". But every time I try to use it, it says that the tool is not a valid member of Backpack. How do I solve this?

local hum = game.Players:GetPlayerFromCharacter(hit.Parent)
if hum and hum.Backpack["infection tool"] == nil then
    local c = hum.Backpack:GetChildren()
    for i = 1, #c do
        c[i]:Remove()
    end
    wait(0.1)
    game.ServerStorage["infection tool"]:Clone().Parent = hum.Backpack
end

This isn't the entire script, but it is the majority of it. It clones perfectly to the player's head for it to work, but the script above is the only code it has right now.

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Backpack isn't an array, it's an instance. So instead of doing

if hum.Backpack["infected tool"] == nil then

do

if hum.Backpack:FindFirstChild("infected tool") == nil then

EDIT: My mistake, you can use either the bottom or top, but the top one will cause an error if it doesn't find anything.

0
There's only one issue, whenever it gets touched, it gives them more than one. Which is the one thing I want to stop. How will I do that? marltonjohnson9876 11 — 5y
0
Try removing the wait(0.1) I don't know if you need it or not, but that's why it's being set more than once. davidgingerich 603 — 5y
0
When the Touch event is activated, the code checks to see if the player has the infect tool, and if the player doesn't, then it waits a tenth of a second and then adds the infect tool. Meanwhile, more touch events can be fired, and since the earlier one is still waiting to add it, the new events will not see any infect tool, and will proceed to add their own infect tool. Also, please try to keep i davidgingerich 603 — 5y
0
Oh, you're referring to my suggestion to use different code. davidgingerich 603 — 5y
1
dont worry, robloxwhiz is just cancer, he gives misinformations 101% of the time so take his opinion with a grain of salt User#24403 69 — 5y
Ad
Log in to vote
0
Answered by
Kymaraaa 116
5 years ago
Edited 5 years ago
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    local Tool = Player.Backpack:FindFirstChild("infection tool") or Player.Character:FindFirstChild("infection tool")
    if Player and not Tool then
        Player.Backpack:ClearAllChildren()
        wait(.1)
        game.ServerStorage["infection tool"]:Clone().Parent = Player.Backpack
    end

The code above should work now.

What I've changed is I've added a variable called Tool, and it'll try to find the tool in the Backpack and Character, if it exists then Tool will be the Infect Tool, if it cannot find the Infect Tool then it'll be equal to nil.

I've also changed your :Destroy() code to Player.Backpack:ClearAllChildren() This line of code accomplishes the same as yours but in a quicker and neater way.

I've also changed hum to Player as you're referencing the Player and not the Humanoid of the Players Character.

Also, when you equip a Tool it goes into your Character. You'll want to check the Character to see if the Tool you're removing from the Backpack also exists there, if it does then you'll want to :Destroy it. I didn't add that code as I do not know the name of the Tool.

0
@RobloxWhizYT, what's your problem? Why are you harassing me? davidgingerich 603 — 5y

Answer this question