Attempt to index boolean with 'Health' - lua

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

Related

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

Check if tool is equipped

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)

Inf timestop glitch in roblox studio ( ServerScriptService.TheWorldServer.TS:35: attempt to index nil with 'Name')

I keep getting this error The error is only for this code. It causes the timestop to be infinite Does anyone know how to fix it? The error is ServerScriptService.TheWorldServer.TS:35: attempt to index
nil with 'Name' Any help is very appreciated.
I have no idea how to fix this so I help is VERY helpful.
local music = game.Workspace.BackgroundMusic
local model = game.ReplicatedStorage.Stand
local InUse = game.ReplicatedStorage.InUse
local bacon = {}
local G = {}
model.BaconTimeStop.OnServerEvent:Connect(function(player)
local find = workspace:GetDescendants()
if InUse.Value == false then
InUse.Value = true
local Sound = script.TSSound:Clone()
Sound.Parent = workspace
Sound:Play()
music:Pause()
wait(1.8)
local TweenService = game:GetService("TweenService")
local part = game.Lighting.ColorCorrection
local goal = {}
goal.Contrast = -2
local tweenInfo = TweenInfo.new(0.2)
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
for i=1, #find do
local That = find[i]
if That:IsA("Part") or That:IsA("MeshPart") then
if That.Anchored == false and That.Parent.Name ~= player.Name and That.Parent.Parent.Name ~= player.Name and That.Name ~= "Baseplate" and not That.Parent:IsA("Accessory") then
That.Anchored = true
table.insert(bacon, That)
end
end
if That:IsA("ParticleEmitter") then
That.TimeScale = 0
table.insert(G, That)
end
end
wait(3)
InUse.Value = false
local TweenService = game:GetService("TweenService")
local part = game.Lighting.ColorCorrection
local goal = {}
goal.Contrast = 0
local tweenInfo = TweenInfo.new(0.2)
local tween = TweenService:Create(part, tweenInfo, goal)
music:Resume()
tween:Play()
for i=1, #bacon do
bacon[i].Anchored = false
end
table.clear(bacon)
for i=1, #G do
G[i].TimeScale = 1
end
table.clear(G)
end
end)
The error message tells you everything you need to know.
ServerScriptService.TheWorldServer.TS:35: attempt to index nil with
'Name'
This is line 35:
if That.Anchored == false
and That.Parent.Name ~= player.Name
and That.Parent.Parent.Name ~= player.Name
and That.Name ~= "Baseplate"
and not That.Parent:IsA("Accessory") then
You index multiple variables with Name as in .Name (indexing operator .).
One of these values is nil.
That cannot be nil as this would cause errors earlier.
print player, That.Parent, and That.Parent.Parent to find out which of them is nil. Then find out why and fix that. We cannot help you here. If it is a valid state simply avoid indexing or replace it by some default value.

How can I implement a debounce in this code

My first thought was to ask on Roblox devforum but since imo they got a really messed up admission system, I might as well ask it here.
I've got a tool that shoots a block (wedge) to wherever the mouse is pointing when clicked. It also casts a ray and the block itself sets the health of any humanoid that makes contact with it to 0. But I have got no idea on how to actually implement a cooldown on the gun so you can't just literally spam blocks that kill anything that touches them arround. I think implementing a debounce here is the best option, but I got stuck with that since day 1 and I have no idea how to write it down correctly
I already tried with most of the things that I thought of after visiting this page Roblox dev page about Debounce, also read through some articles that had similar issues in the dev forum, but I can just spam blocks arround whatever I do.
The tool has just two parts (one being the handle) a localscript to wield the parts together, a localscript to catch the mouse position when clicked, two remote events to pass the info from the localscript to the server script and the following server script
local tool = script.Parent
local clickEvent = tool.ClickEvent
local clickEventConnection
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
--Function that creates the part with a touched listener that kills any humanoid that comes into contact with said block
local function createPart(location)
local part = Instance.new("WedgePart")
part.CFrame = location
part.Parent = workspace
part.BrickColor = BrickColor.new("Black")
part.Touched:connect(function(hit)
if hit.Parent then
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
hum.Health = 0
end
end
end)
game:GetService("Debris"):AddItem(part, 2)
end
--With the information on the click position of the localscript, this function creates a ray and a beam that accompanies the block, as well as executing the createpart() function on said location
local function onClick(player, clickLocation, ignore)
createPart(clickLocation)
local ray = Ray.new(
tool.Handle.CFrame.p,
(clickLocation.p - tool.Handle.CFrame.p).unit * 500
)
local hit, position, normal = workspace:FindPartOnRay(ray, player.Character, ignore)
local beam = Instance.new("Part", workspace)
if player.Team == Teams["Blue Team"] then
beam.BrickColor = BrickColor.new("Bright blue")
elseif player.Team == Teams["Red Team"] then
beam.BrickColor = BrickColor.new("Bright red")
else
beam.BrickColor = BrickColor.new("Ghost grey")
end
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 1)
end
--subscribing onclick() when equiping the weapon and unsubscribig when unequipping it
local function onEquip()
clickEventConnection = clickEvent.OnServerEvent:connect(onClick)
end
local function onUnequip()
clickEventConnection:disconnect()
end
tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)
I just wanted to make a 'cooldown' so a block can be fired each 3 seconds. As is, you can just spam as much as you like to
A simple way to debounce the clicks is to use a variable to decide whether to quickly escape from a function.
You can modify your onClick function so it does not execute if a cooldown is still in place :
-- make a cooldown tracker
local isGunOnCooldown = false
local cooldownTime = 3.0 --seconds
local function onClick(player, clickLocation, ignore)
-- debounce any spammed clicks
if isGunOnCooldown then
return
end
-- put the gun on cooldown
isGunOnCooldown = true
-- fire a bullet
createPart(clickLocation)
local ray = Ray.new(
tool.Handle.CFrame.p,
(clickLocation.p - tool.Handle.CFrame.p).unit * 500)
local hit, position, normal = workspace:FindPartOnRay(ray, player.Character, ignore)
local beam = Instance.new("Part", workspace)
if player.Team == Teams["Blue Team"] then
beam.BrickColor = BrickColor.new("Bright blue")
elseif player.Team == Teams["Red Team"] then
beam.BrickColor = BrickColor.new("Bright red")
else
beam.BrickColor = BrickColor.new("Ghost grey")
end
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 1)
-- start the gun's cooldown and reset it
spawn(function()
wait(cooldown)
isGunOnCooldown = false
end)
end

Resources