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)
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)
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.
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)