Checking if a tool is activated does not work

While trying to detect when a tool is activated, I cannot seem to work it. It says “failed to index nil with activated.” The following is my code from a local script

		PlayerService.LocalPlayer.Backpack:FindFirstChild("Tablet").Activated:Connect(function()
			if PlayerService.LocalPlayer.Backpack:FindFirstChild("Tablet").Equipped then --checks if tool is equipped
				UserInput.InputBegan:Connect(function(key,IsTyping)
					if not IsTyping then
						if key.UserInputType == Enum.UserInputType.MouseButton1 then
							local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
							if Model then
								local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
								if ThePlayer then
									print(ThePlayer)
									print(ThePlayer.UserId)
								end
							end
						end
					end	
				end)
			end
		end)
PlayerService.LocalPlayer.Character:FindFirstChild("Tablet").Activated:Connect(function()
			if PlayerService.LocalPlayer.Character:FindFirstChild("Tablet").Equipped then --checks if tool is equipped
				UserInput.InputBegan:Connect(function(key,IsTyping)
					if not IsTyping then
						if key.UserInputType == Enum.UserInputType.MouseButton1 then
							local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
							if Model then
								local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
								if ThePlayer then
									print(ThePlayer)
									print(ThePlayer.UserId)
								end
							end
						end
					end	
				end)
			end
		end)

When a player is holding a tool it is in the Character not the player’s backpack.

1 Like

it still says “Players.Player1.PlayerGui.StaffUI.Reception.LocalScript:34: attempt to index nil with ‘Activated’”. Not too sure why, as I have already updated it.

Can you send a screenshot of the code? I want to see the lines and variables.

local PlayerService = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local InputService = game:GetService("UserInputService")

local Mouse = PlayerService.LocalPlayer:GetMouse()

local ReceptionValues = Replicated:WaitForChild("ReceptionVals")
local ServiceValue = ReceptionValues.ServiceValue

local Tools = Replicated.Tools
local NormalTools = Tools.NormalTools
local StaffTools = Tools.StaffTools

local Tablet = StaffTools.Tablet

local Event = Replicated.Events
local ReceptionEvent = Event.ReceptionEvent
local StaffToolEvents = Event.StaffItemsEvents

local Reception = script.Parent
local UserInput = Reception.UserText
local ServiceInput = Reception.ServiceText
local Submit = Reception.Submit

local StaffArea = workspace.StaffArea

function _access()
	if PlayerService.LocalPlayer:GetRankInGroup(16844870) >= 0 then
		print("lol")
		StaffToolEvents:FireServer(Tablet)
		PlayerService.LocalPlayer.Character:FindFirstChild("Tablet").Activated:Connect(function()
			if PlayerService.LocalPlayer.Character:FindFirstChild("Tablet").Equipped then --checks if tool is equipped
				UserInput.InputBegan:Connect(function(key,IsTyping)
					if not IsTyping then
						if key.UserInputType == Enum.UserInputType.MouseButton1 then
							local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
							if Model then
								local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
								if ThePlayer then
									print(ThePlayer)
									print(ThePlayer.UserId)
								end
							end
						end
					end	
				end)
			end
		end)
	end
end

You are getting the tool from the Character, not the player.

Do

PlayerService.LocalPlayer.Backpack:FindFirstChild("Tablet").Activated:Connect(function()

Do the same for the line of code beneath, just add the if then and change Activated to Equipped

oh okay! the error also seems to be on

PlayerService.LocalPlayer.Character:FindFirstChild("Tablet").Activated:Connect(function()

as it’s indexing nil with activated

Tools are located on a player’s Backpack, not their character.

Refer to this, which is correct.

Additionally, why so many nested if statements…?

Yes. Index nil means that the thing you are waiting for doesn’t exist and anything after it, and will return nil. Tools are not in the character but in the LocalPlayer. Just replace the Character with Backpack isnstead.

the output states this when I do :waitforchild(), along with also changing it to backpack

Infinite yield possible on 'Workspace.Player1:WaitForChild("Tablet")'

So you are saying that it still returns nil? If then, make sure Tablet is spelled correctly, it is in the Localplayer backpack, parented properly to it, replace the Character with Backpack

You didn’t parent the tablet to the backpack, did you?

It doesn’t return nil. it returns this

Infinite yield possible on ‘Players.Player2.Backpack:WaitForChild(“Tablet”)’

Parent your LocalScript under your specified tool, as you will constantly be receiving this error otherwise. When parented under the tool, you can simply get the tool by doing local Tool = script.Parent. The Activated event will only fire when the Tool is currently equipped.

Edit:

You also do not need UserInputService for this, as the Activated event already fires when the player clicks/taps when the tool is equipped.

How your script should look like:

local PlayerService = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local InputService = game:GetService("UserInputService")

local Mouse = PlayerService.LocalPlayer:GetMouse()

local ReceptionValues = Replicated:WaitForChild("ReceptionVals")
local ServiceValue = ReceptionValues.ServiceValue

local Tools = Replicated.Tools
local NormalTools = Tools.NormalTools
local StaffTools = Tools.StaffTools

local Tablet = StaffTools.Tablet

local Event = Replicated.Events
local ReceptionEvent = Event.ReceptionEvent
local StaffToolEvents = Event.StaffItemsEvents

local Reception = script.Parent
local UserInput = Reception.UserText
local ServiceInput = Reception.ServiceText
local Submit = Reception.Submit

local StaffArea = workspace.StaffArea

function _access()
	if PlayerService.LocalPlayer:GetRankInGroup(16844870) >= 0 then
		print("lol")
		StaffToolEvents:FireServer(Tablet)

		script.Parent.Activated:Connect(function() -- make sure the LocalScript is parented under your tool

			local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
			if Model then
				local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
				if ThePlayer then
					print(ThePlayer)
					print(ThePlayer.UserId)
				end
			end
		end)
	end
end

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