Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to apply a local function to an array?

This part of the script is supposed to make the previously created 5 parts heal what they touch, but Output returns: Attempt to index field 'Touched'(a nil value)

		wait()
		local h = game.Workspace
		local durt = {h.durt1,h.durt2,h.durt3,h.durt4,h.durt5}
		local function onTouch(hit)
			if hit.Parent.FindFirstChild("Humanoid") then
			local hp = hit.Parent.Character.Humanoid.Health
			hp = hp+hp+durt.Size*5
		end
			end
		durt.Touched:connect(onTouch)

full code:

wait()
player = game.Players.LocalPlayer
Torso = player.Character.Torso
human = Torso.Parent.Humanoid
game:GetService("UserInputService").InputBegan:connect(function(inputObject,gameProcessedEvent)
  if inputObject.KeyCode == Enum.KeyCode.E then
      local pos = Torso.Position
      local q,e = {pos.X,pos.Z}
      local w = (pos.Y + 10)
      local anim = Instance.new("Animation",human)
      anim.AnimationId = "https://www.roblox.com/Healing-Move-item?id=439811959"
      local animtrack = human:LoadAnimation(anim)
      animtrack:Play()
      wait(2)	
      local bp = Instance.new("BodyPosition", Torso)
      bp.MaxForce = Vector3.new(0,10000,0)
      bp.P = 10000
      bp.Position = Vector3.new(q,w,e)
      Torso.Parent.Humanoid.WalkSpeed = 0
      wait(2)
      math.randomseed(tick())
      for i = 1,5 do
          local num = i
          i = Instance.new("Part", workspace)
          i.Name = "durt" .. num
          i.Size = Vector3.new(math.random(4),math.random(4),math.random(4))
          local m = math.random(-20,20)
          local a = pos.x+m
          local m = math.random(-20,20)
          local b = pos.y+m
          local m = math.random(-20,20)
          local c = pos.z+m
          i.Transparency = 1
          i.Position = Vector3.new(a,b,c)
          i.CanCollide = false
          i.Shape = 0
          i.BrickColor = BrickColor.new(1020)
          i.TopSurface = 0
          i.RightSurface = 0
          i.LeftSurface = 0
          i.FrontSurface = 0
          i.BottomSurface = 0
          i.BackSurface = 0
          local k = Instance.new("BodyPosition", i)
          k.MaxForce = Vector3.new(50000,50000,50000)
          k.P = 100
          k.D = 100
          w = w+.5
          k.Position = Vector3.new(pos.x,w,pos.z)
      end
      wait()
      local h = game.Workspace
      local durt = {h.durt1,h.durt2,h.durt3,h.durt4,h.durt5}
      local function onTouch(hit)
          if hit.Parent.FindFirstChild("Humanoid") then
          local hp = hit.Parent.Character.Humanoid.Health
          hp = hp+hp+durt.Size*5
      end
          end
      durt.Touched:connect(onTouch)
              local h = game.Workspace
      local durt = {h.durt1,h.durt2,h.durt3,h.durt4,h.durt5}
      for j = 1,0,-.05 do
          wait(.1)
          h.durt1.Transparency = j
          h.durt2.Transparency = j
          h.durt3.Transparency = j
          h.durt4.Transparency = j
          h.durt5.Transparency = j
      end
  end
end)

1 answer

✓ Accepted answer

Your problems

durt.Touched:connect(onTouch)

durt is a list. It therefore does not have a Touched event.

You don't want to apply the event to the list, but the parts within in the list.


 if hit.Parent.FindFirstChild("Humanoid") then

Colons (:) are used for methods in Lua. Therefore, this should be hit.Parent:FindFirstChild


local hp = hit.Parent.Character.Humanoid.Health

hit.Parent is already the character, so it doesn't make sense to write .Character from there. It will just be nil.


 hp = hp+hp+durt.Size*5

You make hp equal to a property (Health). You can't set properties this way. While it will be equal to the value of Health, it won't retain a reference to the object. It is just a number.



##Solution

I didn't read through the rest of your code, but this should help with the snippet you gave. If these same problems arise in other places, be sure to apply the same fixes.

local h = game.Workspace --this variable doesn't make sense
local durt = {h.durt1, h.durt2, h.durt3, h.durt4, h.durt5} --'durt' doesn't make much sense either

for _, part in ipairs(durt) do--loop through the table

	part.Touched:connect(function(hit)--connect an event
		local hum = hit.Parent:FindFirstChild("Humanoid")--find the humanoid
		if hum then
			hum.Health = hum.Health*2 + part.Size*5--edit the health
		end
	end)

end


This discussion is preserved as it was. New answers, comments, and votes are closed.