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)
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).
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.