Script not finding tool even when in inventory

Hi!

I’ve been trying to write a script to detect if a player is holding a tool, but it does not find the tool even when the player is holding it.

local scanevent = game:GetService("ReplicatedStorage"):WaitForChild("Scanned")

script.Parent.Triggered:Connect(function(x)
	local x = game:GetService("Players"):FindFirstChild(x.Name)
	if x then
		print(x.Name, x.Parent.Name)
	end
	if x.Character:WaitForChild("Passport") then
		scanevent:FireClient(x)
		script.Parent.Enabled = false
		script.Parent.Parent.scanned:Play()
		script.Parent.Parent.Parent.astrus.Material = Enum.Material.Neon
		wait(1)
		script.Parent.Parent.scanned:Stop()
		wait(1)
		script.Parent.Parent.Parent.astrus.Material = Enum.Material.Metal
	else
		script.Parent.Enabled = true
		script.Parent.Parent.error:Play()
		wait(1)
		script.Parent.Parent.error:Stop()
	end
end)

When I test the script, it gives me this error:


(You can see that I am holding the tool in the image)

Another picture:
image

I am holding the specified tool, but it is not detected. Could anyone help?

Thanks in advance! :smiley:

1 Like

Replace:

if x.Character:WaitForChild("Passport") then

with

if x.Character:FindFirstChild("Passport") then

I already tried this before, unfortunately it does not work.

Edit: It triggers ^ that part of the script.

Try using FindFirstChild().

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local scanevent = ReplicatedStorage:WaitForChild("Scanned")

script.Parent.Triggered:Connect(function(player)
	local character = player.Character
	local passport = character:FindFirstChild("Passport")
	if passport then
		scanevent:FireClient(player)
		script.Parent.Enabled = false
		script.Parent.Parent.scanned:Play()
		script.Parent.Parent.Parent.astrus.Material = Enum.Material.Neon
		task.wait(1)
		script.Parent.Parent.scanned:Stop()
		task.wait(1)
		script.Parent.Parent.Parent.astrus.Material = Enum.Material.Metal
	else
		script.Parent.Enabled = true
		script.Parent.Parent.error:Play()
		task.wait(1)
		script.Parent.Parent.error:Stop()
	end
end)

I wonder why you weren’t utilizing the player parameter on this event though. So I decided to do it directly. Also check on server view to see if the player is actually holding it from that view, because I think you are viewing it from the client and only client(where things might not be consistent between server and client sometimes).

1 Like

Completed code:

local scanevent = game:GetService("ReplicatedStorage"):WaitForChild("Scanned")

script.Parent.Triggered:Connect(function(x)
	-- no need to check if 'x' exists :)
	if x.Character:FidnFirstChild("Passport") then
		scanevent:FireClient(x)
		script.Parent.Enabled = false
		script.Parent.Parent.scanned:Play()
		script.Parent.Parent.Parent.astrus.Material = Enum.Material.Neon
		task.wait(1)
		script.Parent.Parent.scanned:Stop()
		task.wait(1)
		script.Parent.Parent.Parent.astrus.Material = Enum.Material.Metal
	else
		script.Parent.Enabled = true
		script.Parent.Parent.error:Play()
		task.wait(1)
		script.Parent.Parent.error:Stop()
	end
end)

but I would recommend using @Operatik’s code instead.

You’re right, the tool only shows up on client as I gave it to the player via a LocalScript. Thanks for the help though!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.