Script working alright but errors popping up

heyaa so this silly script is supposted to damage the toucher of the part and then delete itself and it does so almost perfectly but sometimes it just wont damage orr just make errors in the console which i mean its not a big deal but errors are annoying so i need to annihilate them
heres the script

script.Parent.Touched:Connect(function(part)
	if not part.Parent:FindFirstChild("Humanoid") or part.Parent:FindFirstChild("Humanoid").Health <= 0 then
		return
	end
	local hum = part.Parent:WaitForChild("Humanoid")
	if (game.Players:GetPlayerFromCharacter(hum.Parent)) == nil and (hum ~= nil) then
		hum:TakeDamage(game.ReplicatedStorage.weapons.Trowel.damage.Value * game.Players[script.Parent:FindFirstChild("playername").Value].Character.damageBUFF.Value)
		script.Parent:Destroy()
	end
end)

just in case, the script gets added onto the part when it’s spawned
also, forgot to mention that the error is

workspace.part.damager:2: attempt to index nil with findfirstchild (line2)

Here try this:

script.Parent.Touched:Connect(function(part)
	if not part.Parent:FindFirstChild("Humanoid") or part.Parent:FindFirstChild("Humanoid").Health <= 0 then
		return
	end
	local hum = part.Parent:WaitForChild("Humanoid")
	if (game.Players:GetPlayerFromCharacter(hum.Parent)) == nil and (hum ~= nil) then
		local playerName = script.Parent:GetAttribute("playername")
		if playerName then
			local player = game.Players:GetPlayerFromName(playerName)
			if player then
				player.Character:TakeDamage(game.ReplicatedStorage.weapons.Trowel.damage.Value * player.Character.damageBUFF.Value)
			end
		end
		script.Parent:Destroy()
	end
end)

i dont think that would work as the “playername” isnt an attribute, its a stringvalue buut ill try that anyway why not

yeah, as ive said, it still made the error and now it doesnt even deal the damage soo time to revert it

To deal damage to a player, you can use the Humanoid:TakeDamage method instead. Here’s an updated version of your script:


script.Parent.Touched:Connect(function(part)
	if not part.Parent:FindFirstChild("Humanoid") or part.Parent:FindFirstChild("Humanoid").Health <= 0 then
		return
	end
	local hum = part.Parent:WaitForChild("Humanoid")
	if (game.Players:GetPlayerFromCharacter(hum.Parent)) == nil and (hum ~= nil) then
		local playerName = script.Parent:GetAttribute("playername")
		if playerName then
			local player = game.Players:GetPlayerFromName(playerName)
			if player then
				humanoid:TakeDamage(game.ReplicatedStorage.weapons.Trowel.damage.Value * player.Character.damageBUFF.Value)
			end
		end
		script.Parent:Destroy()
	end
end)

This should deal damage to the player’s character when it touches your object. Note that you’ll need to have a damageBUFF attribute on the player’s character for this to work.

Also, as a side note, it’s generally better to use game.Players:GetPlayerFromCharacter instead of checking if the player is nil. This is because the player may be on a different server, and checking if they’re nil may not always return the correct result.

There are a lot of incorrect information here, please do ur research before replying so u don’t mislead the OP.

I would suggest disabling ur script in default, and when u add it, enable ur script.

Most braindead AI response ever. The error isnt evne related to what the AI did,
Change this:

To:

local char = part.Parent
if not char then
		return
	end
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not (humanoid and humanoid.Health > 0) then
return
end

Wrote this on my phone so there might some grammatical issues

1 Like

it was indeed like that, i add the script and enable it when the part spawns

alrighty ill try that right now

thanks a bunch!! it really worked (although i had to change (humanoid and humanoid.Health > 0) to humanoid or humanoid.Health <= 0)
youre a lifesaver!!
also yeah that ai guy was really weird ill probably report them to roblox

1 Like