Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
4 years ago

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)
0
It still errors the same thing as before.. XxGhostBoy_HDxX 38 — 4y
Ad

Answer this question