So, i'm trying to make this game where you run from someone and you can place walls to slow them off. But, it's suposed to kill the player if they spawn too much walls (5) in the span of 10 seconds. But i get the error on title and i don't know how to fix it. (by the way i'm not that great at scripting and my friend who does know lua helped me a bunch, if that matters) Here's the code. It highlights the 9th line, which also means line 22 won't work.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local block = game.Workspace:FindFirstChild("Block", true) local blockCount = 0 local blockTimer = 0 player.CharacterAdded:Connect(function(character) character.Humanoid.MaxHealth = 1000 end) mouse.Button1Down:Connect(function() blockCount = blockCount + 1 local newBlock = block:Clone() newBlock.Name = "Block"..blockCount newBlock.Parent = game.Workspace newBlock.CFrame = player.Character.Head.CFrame - player.Character.Head.CFrame.LookVector if blockCount > 5 and tick() - blockTimer >= 10 then print(player.Character.Humanoid) wait(1) player.Character.Humanoid:TakeDamage(1000) blockCount = 0 blockTimer = 0 end) delay(15, function() newBlock:Destroy() blockCount = blockCount - 1 end) blockTimer = tick() end) game:GetService("RunService").Heartbeat:Connect(function() if tick() - blockTimer >= 10 then blockCount = 0 blockTimer = 0 end end) print(player)
Hello! The answer is the humanoid has not loaded yet. The character has, but it's still loading the humanoid, body parts, body color, etc.. It's a pretty easy fix though. Using :WaitForChild()
you can yield the function (pause it) until it is found. If it has not found it within 5 seconds (which is the default), it will end the function.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local block = game.Workspace:FindFirstChild("Block", true) local blockCount = 0 local blockTimer = 0 player.CharacterAdded:Connect(function(character) character:WaitForChild("Humanoid",5).MaxHealth = 1000 -- the 5 inside WaitForChild says how long to yield for until cancelling the wait, math.huge will make it yield permanently until it finds it end) mouse.Button1Down:Connect(function() blockCount = blockCount + 1 local newBlock = block:Clone() newBlock.Name = "Block"..blockCount newBlock.Parent = game.Workspace newBlock.CFrame = player.Character.Head.CFrame - player.Character.Head.CFrame.LookVector if blockCount > 5 and tick() - blockTimer >= 10 then print(player.Character:WaitForChild("Humanoid") wait(1) player.Character:WaitForChild("Humanoid"):TakeDamage(1000) blockCount = 0 blockTimer = 0 end) delay(15, function() newBlock:Destroy() blockCount = blockCount - 1 end) blockTimer = tick() end) game:GetService("RunService").Heartbeat:Connect(function() if tick() - blockTimer >= 10 then blockCount = 0 blockTimer = 0 end end) print(player)
Also, you are running this from a LocalScript. If this is a multiplayer game, it wouldn't kill you on everybody else's screen. You can look into PlayerAdded
for a pretty easy solution on a normal Script. Also, I dont see the need for setting the health to 1000 only to deal 1000 damage. Setting the health to 0 would be a guaranteed kill regardless of ForceFields or hacks. Hope this helps!