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

So i created this Damage display script, but it works only once and after that time it errors?

The Damage Display script-

local isUsing = false
local tool = script.Parent.Parent
local Blade1 = script.Parent.Parent.Union
local Blade2 = script.Parent.Parent.Union2
local Blades = Blade1 or Blade2
local DamageDis = script.Parent.Parent.Instances.DamageDisplay:Clone()
local Damage = script.Parent.Parent.Values.Damage
local DamageText = script.Parent.Parent.Instances.DamageDisplay.Damage:Clone()
local CanDamage = script.Parent.Parent.Values.CanDamage
local Attachment = Instance.new("Attachment")

tool.Activated:Connect(function()
isUsing = true
wait(2)
isUsing = false
end)

Blades.Touched:Connect(function(hit)
	local HumanoidRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
	  if isUsing and HumanoidRoot and CanDamage.Value == true then
		Attachment.Parent = HumanoidRoot
		DamageDis.Parent = Attachment
		DamageText.Parent = DamageDis
		DamageText.Text = tostring(Damage.Value)
		DamageDis.Enabled = true
		wait(2)
		DamageDis:Destroy()
		DamageText:Destroy()
		Attachment:Destroy()
  end
end)

And the error is - The Parent property of Attachment is locked, current parent: NULL, new parent HumanoidRootPart

1 answer

You only created one instance of an Attachment object. On line 29, you destroy that object the first time .Touched fires, and therefore there are no more attachments to destroy (hence the NULL error). To fix this, create a new attachment whenever the .Touched event fires.

local isUsing = false
local tool = script.Parent.Parent
local Blade1 = script.Parent.Parent.Union
local Blade2 = script.Parent.Parent.Union2
local Blades = Blade1 or Blade2
local DamageDis = script.Parent.Parent.Instances.DamageDisplay:Clone()
local Damage = script.Parent.Parent.Values.Damage
local DamageText = script.Parent.Parent.Instances.DamageDisplay.Damage:Clone()
local CanDamage = script.Parent.Parent.Values.CanDamage

tool.Activated:Connect(function()
isUsing = true
wait(2)
isUsing = false
end)

Blades.Touched:Connect(function(hit)
    local HumanoidRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
      if isUsing and HumanoidRoot and CanDamage.Value == true then
        local Attachment = Instance.new("Attachment")
        Attachment.Parent = HumanoidRoot
        DamageDis.Parent = Attachment
        DamageText.Parent = DamageDis
        DamageText.Text = tostring(Damage.Value)
        DamageDis.Enabled = true
        wait(2)
        DamageDis:Destroy()
        DamageText:Destroy()
        Attachment:Destroy()
  end
end)

1 comment

0

It still errors the same thing as before..

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