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