Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script that checks if someone has a tool?

So I have a scanner that checks for certain tools, such as weapons and tells the operator if they are armed or not on a textlabel. The scanner scans everyone in a room. It keeps saying my script bellow is attempting to call a nil value.

local Players = game:GetService("Players")
local armed = false
local text = script.Parent.SurfaceGui.TextButton.Text
 
local part = script.Parent
local function onTouched(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if player then 
		local Backpack = player.Backpack
		print(player.Name)
		armed = false
		if Backpack:FindFirstChild("M4A1") then 
			armed = true
		end
		if Backpack:FindFirstChild("AK47") then 
			armed = true
		end
		if Backpack:FindFirstChild("Baton") then 
			armed = true
		end
		if Backpack:FindFirstChild("Pistol") then
			armed = true
		end
		if Backpack:FindFirstChild("Rifle") then 
			armed = true
		end
		if armed == true then 
			text = "ARMED"
		else
			text = "UNARMED"
		end
	end
end
part.Touched:Connect(onTouched)

1 answer

✓ Accepted answer

Your issue may be the double use of the variable part. You are using this variable as a reference to the part that needs to be touched but also using the variable to show a part that was found touching.

I suggest completely remove local part = script.Parent because you are using this variable once, not several times. Please see below for the updated script:

local Players = game:GetService("Players")
local armed = false
local text = script.Parent.SurfaceGui.TextButton.Text

local function onTouched(hit) -- I changed for hit to make it more obvious what this variable represents
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if player then 
        local Backpack = player.Backpack
        print(player.Name)
        armed = false
        if Backpack:FindFirstChild("M4A1") or player.Character:FindFirstChild("M4A1") then 
            armed = true
        end
        if Backpack:FindFirstChild("AK47") or player.Character:FindFirstChild("AK47") then 
            armed = true
        end
        if Backpack:FindFirstChild("Baton") or player.Character:FindFirstChild("Baton") then 
            armed = true
        end
        if Backpack:FindFirstChild("Pistol") or player.Character:FindFirstChild("Pistol") then
            armed = true
        end
        if Backpack:FindFirstChild("Rifle") or player.Character:FindFirstChild("Rifle") then 
            armed = true
        end
        if armed == true then 
            text = "ARMED"
        else
            text = "UNARMED"
        end
    end
end
part.Touched:Connect(onTouched)

You may have noticed I am checking through Character. This is due to the Tool being moved from Backpack to the Character Model when you equip a tool, meaning your code will give "UNARMED" as a result if the only tool you have is equiped.

Comment any issues or question you have below

This discussion is preserved as it was. New answers, comments, and votes are closed.