Every time I use my script:
script.Parent.Touched:Connect(function(tch) if tch.Parent:FindFirstChild("Humanoid") then tch.Parent.Humanoid.Health = tch.Parent.Humanoid.Health-math.random(1,10) wait(10) end end)
I get the following error
Workspace.Part.DamageScript:3: attempt to perform arithmetic on field 'Health' (a userdata value)
If someone would help me I would be extremely grateful!
Try defining the humanoid first, and then using the :TakeDamage() event
script.Parent.Touched:Connect(function(tch) local h = tch.Parent:FindFirstChild("Humanoid") if h ~= nil then h:TakeDamage(math.random(1,10)) wait(10) end end)
Hope this solved your problem, if not let me know we might be able to figure something else out.
I have made a script like that before but it slowkills or it can insta kill
function OnTouch(hit) hit.Parent.Humanoid:TakeDamage(5) end script.Parent.Touched:Connect(OnTouch)(hit)
should work you may get an error but thats only because the part might be touching the baseplate
This should work for all players:
local part = script.Parent local debounce = true part.Touched:Connect(function(tch) if tch and tch.Parent and game:GetService("Players"):GetPlayerFromCharacter(tch.Parent) and debounce == true then then debounce = false local human = tch.Parent:FindFirstChildOfClass("Humanoid") if human then human.Health = human.Health - math.random(1, 10) else print("Humanoid Does Not Exist") end wait(10) debounce = true end end)
I'm using the GetPlayerFromCharacter(character)
function of the Players
Service to make sure that the object touched is from a player in the game
I'm using the FindFirstChildOfClass(ClassName)
function to make sure you are accurately finding the Humanoid, as you may have changed the name of it.
The debounce is since you have a wait(10) I assume you only want this to run every 10 seconds.