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

My KeyDown() Function Works The First Time, But Breaks After Multiple Presses?

Thank you for looking at my question, this is a very complicated problem.

The Problem...

The problem that I am encountering in my KeyDown() code is that the first press of the code works, the "Orb" works (including the damage), the chat works, and the animation plays smoothly (sort of, working on that...) and the code does what it suppose to do. After the second press however, the chat works, but the animation and the "Orb" do not show up at all. I waited to press the KeyDown()(which is z) again, but as I pressed, I got an error that read "The Parent property of Damage is locked, current parent: NULL, new parent GreekGodOfMLG ". I've never seen an error like this before in my entire coding days (which has been 3 years now) and I'm dumbfounded on the issue as well.

My Setup...

My setup of the code is the main code called "DarkOrb" is in the StarterPack, and the code is in a local script. I have a child of the code, which is called "Damage" and as the name serves, it gives the "Orb" damage.

The Pieces of Code

The first piece of code is the local script and serves as what I like to call the "Mainframe" of all the code. Here it is:

--Parent Of Script: StarterPack

--Labels

local Player = game.Players.LocalPlayer
local Humanoid = Player.Character.Humanoid
local Character = Player.Character
local Mouse = Player:GetMouse()
local Run = game:GetService('RunService')
local DarkOrb = Instance.new('Part', workspace)
local DarkFire = Instance.new('Fire', DarkOrb)
local Force = Instance.new('BodyVelocity', DarkOrb)
local Damage = script.Damage:clone()
local Animation = Instance.new('Animation')
Animation.AnimationId = 'https://www.roblox.com/DarkOrb-Animation-item?id=464893423'
local Track = nil
local CanPlay = true

--DarkOrb Function/Details

function onKeyDown(Key)
	Key = Key:lower()
	if Key == 'z' then		
		DarkOrb.BrickColor = BrickColor.Black()
		DarkOrb.Material = 'CorrodedMetal'
		DarkOrb.CanCollide = true
		DarkOrb.Size = Vector3.new(10, 10, 10)
		DarkOrb.TopSurface = 'Smooth'
		DarkOrb.BottomSurface = 'Smooth'
		DarkOrb.Shape = 'Ball'
		DarkOrb.Transparency = 0.7
		DarkOrb.Name = Player.Name
		DarkOrb.CFrame = Player.Character.Torso.CFrame * CFrame.new(0, 0 ,-10)

--Animation Details
  if CanPlay then
	 CanPlay = false
	 Track = Humanoid:LoadAnimation(Animation)
	 Track.KeyframeReached:connect(function(keyframename)
		if keyframename == 'DarkOrb Animation' then
			CanPlay = true
		end
	 end)
	Track:Play()
  end
		game:GetService('Chat'):Chat(Player.Character.Head, 'Feel The Darkness...DARK ORB!!!')

--DarkFire Details

		DarkFire.Color = Color3.new(0, 0, 0)
		DarkFire.Heat = 100
		DarkFire.SecondaryColor = Color3.new(85, 0, 255)
		DarkFire.Size = 100
		
--Force Details

  Force.maxForce = Vector3.new(math.huge, math.huge, math.huge)
  Force.Velocity = Player.Character.Torso.CFrame.lookVector*85
  game.Debris:AddItem(DarkOrb, 3)

--Damage Details (Child Of Script)

  Damage.Parent = DarkOrb
  Damage.Disabled = false
	end
end
--Recalling Functions

Mouse.KeyDown:connect(onKeyDown)

Here is the second code in the script (Damage) that is the child of the first code

--Labels

local DarkBlast = script.Parent

--Damage Function

function onDamage(Dark)
	if Dark.Parent:FindFirstChild('Humanoid') ~= nil and Dark.Parent.Name ~= 'script.Parent.Name' then
		script.Disabled = true
		for i = 1,4 do
		Dark.Parent.Humanoid.Health = Dark.Parent.Humanoid.Health -15
		wait(0.05)
		end
		script.Parent:Remove()
	end
	wait(0.025)
end

--Recall Functions

DarkBlast.Touched:connect(onDamage)

So...

Can you guys help me? This error is very intresting to me and I would like an answer so that I can use this as a future reference in case I come across this error again. Thanks for looking at my question, have a good day.

Greek

1 comment

0

Did you mean to disable the "Damage" script before causing damage to the person?

1 answer

First, I suggest that you change the Mouse.KeyDown() to the UserInputService.InputBegan function.

I edited your code a bit, It might not work, I havn't tested it, but you can try.

--Parent Of Script: StarterPack

--Labels

local Player = game.Players.LocalPlayer
local Humanoid = Player.Character.Humanoid
local Character = Player.Character
-- local Mouse = Player:GetMouse() ; Not needed
local Run = game:GetService('RunService')

--DarkOrb Function/Details

function onKeyDown(input)
    local key = input.KeyCode
    if key == Enum.KeyCode.Z then      
local DarkOrb = Instance.new('Part', workspace)
        DarkOrb.BrickColor = BrickColor.Black()
        DarkOrb.Material = 'CorrodedMetal'
        DarkOrb.CanCollide = true
        DarkOrb.Size = Vector3.new(10, 10, 10)
        DarkOrb.TopSurface = 'Smooth'
        DarkOrb.BottomSurface = 'Smooth'
        DarkOrb.Shape = 'Ball'
        DarkOrb.Transparency = 0.7
        DarkOrb.Name = Player.Name
        DarkOrb.CFrame = Player.Character.Torso.CFrame * CFrame.new(0, 0 ,-10)

--Animation Details

local Animation = Instance.new('Animation')
Animation.AnimationId = 'http://www.roblox.com/Asset?ID=464893423'
local Track = nil
local CanPlay = true

  if CanPlay then
     CanPlay = false
     Track = Humanoid:LoadAnimation(Animation)
     Track.KeyframeReached:connect(function(keyframename)
        if keyframename == 'DarkOrb Animation' then
            CanPlay = true
        end
     end)
    Track:Play()
  end
        game:GetService('Chat'):Chat(Player.Character.Head, 'Feel The Darkness...DARK ORB!!!')

--DarkFire Details
local DarkFire = Instance.new('Fire', DarkOrb)
        DarkFire.Color = Color3.new(0, 0, 0)
        DarkFire.Heat = 100
        DarkFire.SecondaryColor = Color3.new(85, 0, 255)
        DarkFire.Size = 100

--Force Details
local Force = Instance.new('BodyVelocity', DarkOrb)
  Force.maxForce = Vector3.new(math.huge, math.huge, math.huge)
  Force.Velocity = Player.Character.Torso.CFrame.lookVector*85
  game.Debris:AddItem(DarkOrb, 3)

--Damage Details (Child Of Script)

local Damage = script.Damage:clone()
  Damage.Parent = DarkOrb
  Damage.Disabled = false
    end
end
--Recalling Functions

game:GetService('UserInputService'):connect(onKeyDown)

EDIT:

The issue was that the script only created only one object of each every time you ran the script(1 DarkOrb, 1 DarkFire, 1 damage script), and the script only knew these objects.

And when the objects were "used"(pressing the Z key first time), they were deleted, second time the script didn't found these objects.(they were gone), so the script gave error, didn't know what to do.

The fast-fix I did was to create those objects every time you pressed Z(The objects will be created and used every time they are needed.)

Sorry if you can't understand it. I'm terrible at explaining.

4 comments

2

Might want to explain to him what the issue was...

0

I had to edit parts of your code due it having an an error at the bottom, and I fixed it. However, I still don't see what the issue was, could you or someone else explain it to me?

0

And the code worked too by the way.

0

I edited my answer

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