Check if tool is equipped - lua

How do I determine in a condition if a tool is currently equipped?
The following is the LocalScript in my tool, called "Axe".
local UIS = game:GetService("UserInputService")
local Animation = script.Chop
local Player = game.Players.LocalPlayer
local Character = Player.Character
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local A = Humanoid:LoadAnimation(Animation)
A:Play()
end
end)

Tools are parented under the character when equipped. Henceforth, you can just write the following condition:
if Player.Character:FindFirstChild("Axe") ~= nil then

you can make a variable that can store a boolean and changes when the tool is equipped or unequipped
local isEquipped = false
Tool.Equipped:Connect(function()
isEquipped = true
end)
Tool.Unequipped:Connect(function()
isEquipped = false
end)

Related

Attempt to index boolean with 'Health'

I made a punch script, with a hitbox part, but it doesn't gives damage and i have an error in the output called "Attempt to index boolean with 'Health'", but the humanoid exists and his properties too, what is happening? Can i fix that? Code here:
local rep = game:GetService("ReplicatedStorage")
local debounceDMG = true
rep.Combate.Soco.OnServerEvent:Connect(function(plr)
local math = math.random(1,2)
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char.Humanoid
local lanim = char.Humanoid:LoadAnimation(script.Left)
local lanim1 = char.Humanoid:LoadAnimation(script.Animation)
local hitbox = Instance.new("Part")
hitbox.Parent = workspace
hitbox.Transparency = 0
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Size = Vector3.new(2.5,2.5,2.5)
hitbox.CanTouch = true
local c = game:GetService("RunService").Heartbeat:Connect(function()
if math == 2 then
hitbox.Position = char.LeftHand.Position
elseif math == 1 then
hitbox.Position = char.RightHand.Position
end
end)
if math == 2 then
lanim:Play()
elseif math == 1 then
lanim1:Play()
end
hitbox.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid") ~= Humanoid
if hum then
hum.Health -= 5
end
end)
task.wait(.3)
hitbox:Destroy()
c:Disconnect()
end)
This line:
local hum = hit.Parent:FindFirstChild("Humanoid") ~= Humanoid
assigns a boolean to hum, depending on whether the return value from FindFirstChild is not equal to Humanoid.
Because hum is a boolean, that's why you get the Attempt to index boolean with 'Health' runtime error on this line:
hum.Health -= 5
First, I'd recommend adding this hot comment to the very top of your script:
--!strict. Then, Luau LSP (if you're using Visual Studio Code) and/or Roblox Studio can tell you about this mistake before your code even runs. It's really nice to see the red squiggle appear and tell you more precisely what's wrong.
Second, you probably mean for the hum assignment to simply be:
local hum = hit.Parent:FindFirstChild("Humanoid")

"attepted to index nil with Humanoid"

local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local human = char.Humanoid
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end
end)
this script keeps returning error "attempted to index nil with huamnoid" i have looked for solutions on dev forum and on stack overflow but i could not find anything so im going to post myself. anyone catch any erros?
"attempted to index nil with huamnoid"
What that means is that this script loaded before the player's character loaded in.
To fix this, you can use
local char = player.Character or player.CharacterAdded:Wait()
-- uses the player if it's already loaded in, or waits for the character to load in
Also make sure to use
local human = char:WaitForChild("Humanoid")
-- to make sure you wait till the humanoid gets added
Final script
local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end)

Shortcutting script, touch function

I'm trying to have a better/shorter way to decrease any lag and use less effort, as if I need to change anything in the script, I'll need to do it for each one.
Is there a better way of having short?
I tried having them the "Glass1's" the same name and the "Glass2's" the same name, but it worked on the first one only, I wish I clarified it.
Here is my code:
local End = script.Parent.End
local Start = script.Parent.Start
local Glass = script.Parent
--Glass1/1-8 are the glasses that fall if touched and they change color to red
local function TouchedGlass11(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass11
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass11.Touched:Connect(TouchedGlass11)
local function TouchedGlass12(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass12
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass12.Touched:Connect(TouchedGlass12)
local function TouchedGlass13(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass13
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass13.Touched:Connect(TouchedGlass13)
local function TouchedGlass14(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass14
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass14.Touched:Connect(TouchedGlass14)
--then I'll do Glass2/1-8 which just turn the brick to green.
local function TouchedGlass21(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass21
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass21.Touched:Connect(TouchedGlass21)
local function TouchedGlass22(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass22
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass22.Touched:Connect(TouchedGlass22)
local function TouchedGlass23(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass23
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass23.Touched:Connect(TouchedGlass23)
local function TouchedGlass24(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass24
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass24.Touched:Connect(TouchedGlass24)
--Does anyone know a better way?
please note: there is 8 glasses in both "Glass2" and "Glass1" but I'll be adding more soon, that's why I'm looking for an easier way.
Looking at your code it's pretty easy to spot that your using a lot of touched functions.
You can condense this significantly by using a for loop which iterates through each of the parts in script.Parent.
local glass = script.Parent
-- iterate through each child and assign the child to the variable object
for _, object in pairs(glass:GetChildren()) do
-- Make sure this child of script.Parent is actually a part.
if object:IsA("Part") then
object.Touched:Connect(function(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = object
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
end
end)
end
end
You also shouldn't need to have a return in your touched functions.

attempt to index nil with 'LoadCharacter'

When trying to respawn the player with plr:LoadCharacter() it just gives me:
attempt to index nil with 'LoadCharacter' & I tried multiple ways to do it such as Player:LoadCharacter() or is there a more efficient way to kill/respawn the player?
--Declared Boolean Global variable
_G.TimerStart = false
--Local Paths to Objeccts
local label = game.StarterGui.TimerGUI.Timer
-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
--Get Children
local Child = game.Players:GetChildren()
-- Wait for Child Variables
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
local timerevent = ReplicatedStorage:WaitForChild("Timer")
local function Thieves(Players)
if _G.TimerStart == false then
for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
for i,v in pairs(game.Teams.Police:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
end
end
In this case plr is a nil value. So plr:LoadCharacter() is not allowed as it does not make any sense.
local plr = game:GetService("Players").LocalPlayer
is the reason.
So refer to this manual page: https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer
Maybe this helps:
Loading GUIs When creating loading GUIs using ReplicatedFirst, sometimes a LocalScript can run before the LocalPlayer is available.
In this case, you should yield until it becomes available by using
Instance:GetPropertyChangedSignal
local Players = game:GetService("Players")
-- Below: access Players.LocalPlayer; if it is nil, we'll wait for it using GetPropertyChangedSignal.
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

Roblox Lua cannot access modulescript function

I've been trying to connect a ModuleScript to a server Script, and it's throwing the error: Workspace.Script:6: attempt to call field 'check3' (a nil value)
ModuleScript
local GunStats = {}
local part = workspace:WaitForChild('Mosin'):WaitForChild("Union")
local billboard = workspace:WaitForChild('BillboardPart'):WaitForChild('BillboardGui')
local uis = game:GetService("UserInputService")
local Ekey = Enum.KeyCode.E
local check = false
local start = tick()
local function onpress(action1)
if check then
part.BrickColor = BrickColor.new("Black")
end
end
local function isKeydown(startTime)
return uis:IsKeyDown(startTime) and startTime - tick() <= 5
end
local function Input(input, gameprocessed)
if isKeydown(Ekey) then
print("h")
else
print("n")
end
start = tick()
end
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humRoot = character:WaitForChild("HumanoidRootPart")
uis.InputBegan:Connect(Input)
GunStats.check3 = function()
while wait() do
if (humRoot.Position - part.Position).Magnitude < 5 then
print("IN RANGE")
check = true
billboard.Enabled = true
repeat wait() until (humRoot.Position - part.Position).Magnitude >= 5
elseif (humRoot.Position - part.Position).Magnitude > 5 and check then
check = false
print("OUT OF")
billboard.Enabled = false
end
end
end
end)
return GunStats
Server Script:
local players = game:GetService("Players")
local serverStorage = game.ServerStorage
local gunStats = require(serverStorage:WaitForChild("ModuleScript"))
game.Players.PlayerAdded:Connect(function(players)
gunStats.check3(players)
end)
This issue is because the gunStats.check3() function isn't defined on the GunStats object until after a player joins. I would restructure your ModuleScript so that GunStats.check3() is defined right away :
--[[ define all your helper functions up here ... ]]
local GunStats = {}
function GunStats.check3(player)
-- access the humanoid
local character = player.Character or player.CharacterAdded:Wait()
local humRoot = character:WaitForChild("HumanoidRootPart")
-- do the checks
end
return GunStats

Resources