Not creating parts even if the value is above 1?

I am trying to make a spell power for a rpg game I am making, the problem is, it stops creating spell beams (parts) after the spellpower value is above 1.
It is a script inside of a tool.

local tool = script.Parent
local Player = tool.Parent.Parent
local damage = 25
local SpellPower = 10
local Debounce = false
tool.Activated:Connect(function()
	if SpellPower > 1 and Debounce == false then
	   print(SpellPower)
	   Debounce = true	
        local Beam = Instance.new("Part", game.Workspace)
        Beam.CFrame = Player.Character.RightHand.CFrame + Vector3.new(2,2,2)
        Beam.Shape = Enum.PartType.Cylinder
        Beam.TopSurface = Enum.SurfaceType.Smooth
        Beam.BottomSurface = Enum.SurfaceType.Smooth
        Beam.Transparency = 0.2
        Beam.BrickColor = BrickColor.new("Plum")
        local velocity = Instance.new("BodyVelocity")
        velocity.Parent = Beam
        velocity.Velocity = script.Parent.Parent.Head.CFrame.lookVector * 120
	   Beam.Orientation = Vector3.new(0,0,0)
	   wait()
        Beam.Parent = workspace
        Beam:SetNetworkOwner(nil)
        wait()
        Beam.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print('Humanoid is taking damage!')	
			local humanoid = hit.Parent:FindFirstChild('Humanoid')	
               humanoid.Health = humanoid.Health - damage
               Beam:Destroy()
			end
		SpellPower = SpellPower - 1
		wait(3)
		Debounce = false
		end)
    end
end)

there are no errors.

Ok, so I see you have a print statement in there. Does that ever get printed? The one that prints the value of SpellPower

1 Like

It prints the spellpower once, and then just stops

It is because of the Touched function. Don’t put a function inside another function! Create the Beam, put it in Replicated storage then change to

Beam = game.ReplicatedStorage.Beam

After you do that, create a script inside the Beam and write this

local Beam = script.Parent
Beam.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print('Humanoid is taking damage!')	
			local humanoid = hit.Parent:FindFirstChild('Humanoid')	
               humanoid.Health = humanoid.Health - damage
               Beam:Destroy()
end)
1 Like

I notice that when you define player, you are actually getting the character, and not the player, since when you equip a tool, it goes into the character, and doesn’t stay in the backpack.

1 Like

When the game starts, the tool is inside the player’s backpack.

2 Likes

Ohh! My bad, I was thinking that this variable is changing. You’re absolutely right.

Beam = game.ReplicatedStorage.Beam

why would I do this if I am creating an Instance.new part?

Okay, after looking at this much closer, you are changing the debounce back to false only if they get hit by the beam.

local tool = script.Parent
local Player = tool.Parent.Parent
local damage = 25
local SpellPower = 10
local Debounce = false
tool.Activated:Connect(function()
	if SpellPower > 1 and Debounce == false then
	   print(SpellPower)
	   Debounce = true	
        local Beam = Instance.new("Part", game.Workspace)
        Beam.CFrame = Player.Character.RightHand.CFrame + Vector3.new(2,2,2)
        Beam.Shape = Enum.PartType.Cylinder
        Beam.TopSurface = Enum.SurfaceType.Smooth
        Beam.BottomSurface = Enum.SurfaceType.Smooth
        Beam.Transparency = 0.2
        Beam.BrickColor = BrickColor.new("Plum")
        local velocity = Instance.new("BodyVelocity")
        velocity.Parent = Beam
        velocity.Velocity = script.Parent.Parent.Head.CFrame.lookVector * 120
	   Beam.Orientation = Vector3.new(0,0,0)
	   wait()
        Beam.Parent = workspace
        Beam:SetNetworkOwner(nil)
        wait()
        Beam.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print('Humanoid is taking damage!')	
			local humanoid = hit.Parent:FindFirstChild('Humanoid')	
               humanoid.Health = humanoid.Health - damage
               Beam:Destroy()
			end
		
		end)
       SpellPower = SpellPower - 1
       wait(3)
      Debounce = false
    end
end)

That should solve your problem.

1 Like

You should just create the Beam manually and put it in ReplicatedStorage, then insert a script inside it and add the touched function. As I said, you shouldn’t put a function inside another function, so we create a script where we write the function. This should be the main script

local tool = script.Parent
local Player = tool.Parent.Parent
local damage = 25
local SpellPower = 10
local Debounce = false

tool.Activated:Connect(function()
	if SpellPower > 1 and Debounce == false then
	   print(SpellPower)
	   Debounce = true	
        local Beam = game.ReplicatedStorage:WaitForChild("Beam"):Clone()
        Beam.CFrame = Player.Character.RightHand.CFrame + Vector3.new(2,2,2)
        local velocity = Instance.new("BodyVelocity")
        velocity.Parent = Beam
        velocity.Velocity = script.Parent.Parent.Head.CFrame.lookVector * 120
	   Beam.Orientation = Vector3.new(0,0,0)
		   wait()
		print("1")
        Beam.Parent = workspace
        Beam:SetNetworkOwner(nil)
		        wait()

		SpellPower = SpellPower - 1
			wait(3)
			print("Setting deb to false")
		Debounce = false
    end
end)

This should be the script inside the Beam

local Beam = script.Parent
Beam.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print('Humanoid is taking damage!')	
			local humanoid = hit.Parent:FindFirstChild('Humanoid')	
               humanoid.Health = humanoid.Health - damage
               Beam:Destroy()
end)

it works, however it only works 5 times this time instead of 1

Oh, nevermind it was because I put spellpower in the wrong area