The problem is that is works where I click but does not follow my mouse.
local targetAttach = script.Parent:WaitForChild("CannonTarget")
local tool = script.Parent.Parent
tool.Activated:connect(function()
if script.Parent.Beam0.Enabled == false then
local mouse = game.Players.LocalPlayer:GetMouse()
local hit = mouse.Hit
local obj = mouse.Target
targetAttach.Parent = game.Workspace.Terrain
targetAttach.Position = hit.p
script.Parent.Beam0.Enabled = true
script.Parent.Beam1.Enabled = true
script.Parent.Sound:Play()
while wait(.001) do
targetAttach.Position = hit.p
end
if obj then
print("obj")
if obj.Parent:FindFirstChild("Humanoid") then
print("hum")
local ws = obj.Parent.Humanoid.WalkSpeed
obj.Parent.Humanoid.WalkSpeed = 0
for i = 1, 50 do
obj.Parent.Humanoid:TakeDamage(2)
wait(0.1)
end
obj.Parent.Humanoid.WalkSpeed = ws
else
wait(2.7)
end
else
wait(2.7)
end
script.Parent.Sound:Stop()
targetAttach.Parent = script.Parent
targetAttach.Position = script.Parent.Position
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
end
end)
tool.Unequipped:connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
end)
tool.Equipped:connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
end)
local t = 0
game:GetService("RunService").RenderStepped:connect(function()
t = t + 1
if t % 10 == 0 then
script.Parent.Beam0.TextureSpeed = math.random(1, 400)
script.Parent.Beam1.TextureSpeed = math.random(1, 400)
script.Parent.Beam2.TextureSpeed = math.random(1, 400)
elseif t % 10 == 1 then
script.Parent.Beam0.TextureSpeed = 0
script.Parent.Beam1.TextureSpeed = 0
script.Parent.Beam2.TextureSpeed = 0
end
if t > 9999 then t = 0 end
end)
Few things:
You need to add a loop that continously moves the beam to the mouse position. You can’t just call it once
while wait(.001) do
targetAttach.Position = hit.p
end
prevents anything after it from firing. Move it to the end OR put it in a spawn(function()while wait() do end )end)
Also, wait(.001) does the same thing as wait(), and the same thing as wait(0.03) (not exactly 0.3, but it would be 1/30
local targetAttach = script.Parent:WaitForChild("CannonTarget")
local tool = script.Parent.Parent
tool.Activated:connect(function()
if script.Parent.Beam0.Enabled == false then
local mouse = game.Players.LocalPlayer:GetMouse()
local hit = mouse.Hit
local obj = mouse.Target
targetAttach.Parent = game.Workspace.Terrain
targetAttach.Position = hit.p
script.Parent.Beam0.Enabled = true
script.Parent.Beam1.Enabled = true
script.Parent.Sound:Play()
spawn(function()
while wait() do
targetAttach.Position = hit.p
end
end)
if obj then
print("obj")
if obj.Parent:FindFirstChild("Humanoid") then
print("hum")
local ws = obj.Parent.Humanoid.WalkSpeed
obj.Parent.Humanoid.WalkSpeed = 0
for i = 1, 10 do
obj.Parent.Humanoid:TakeDamage(5)
wait(0.1)
end
obj.Parent.Humanoid.WalkSpeed = ws
else
wait(2.7)
end
else
wait(2.7)
end
script.Parent.Sound:Stop()
targetAttach.Parent = script.Parent
targetAttach.Position = script.Parent.Position
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
end
end)
tool.Unequipped:connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
end)
tool.Equipped:connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
end)
local t = 0
game:GetService("RunService").RenderStepped:connect(function()
t = t + 1
if t % 10 == 0 then
script.Parent.Beam0.TextureSpeed = math.random(1, 400)
script.Parent.Beam1.TextureSpeed = math.random(1, 400)
script.Parent.Beam2.TextureSpeed = math.random(1, 400)
elseif t % 10 == 1 then
script.Parent.Beam0.TextureSpeed = 0
script.Parent.Beam1.TextureSpeed = 0
script.Parent.Beam2.TextureSpeed = 0
end
if t > 9999 then t = 0 end
end)
You should disconnect the function when unequipped.
Final:
local targetAttach = script.Parent:WaitForChild("CannonTarget")
local tool = script.Parent.Parent
local Heartbeat = nil
tool.Activated:Connect(function()
if script.Parent.Beam0.Enabled == false then
local mouse = game.Players.LocalPlayer:GetMouse()
local obj = mouse.Target
targetAttach.Parent = game.Workspace.Terrain
script.Parent.Beam0.Enabled = true
script.Parent.Beam1.Enabled = true
script.Parent.Sound:Play()
Heartbeat = game:GetService("RunService").Heartbeat:Connect(function()
targetAttach.Position = mouse.Hit.p
end)
if obj then
print("obj")
if obj.Parent:FindFirstChild("Humanoid") then
print("hum")
local ws = obj.Parent.Humanoid.WalkSpeed
obj.Parent.Humanoid.WalkSpeed = 0
for i = 1, 10 do
obj.Parent.Humanoid:TakeDamage(5)
task.wait(0.1)
end
obj.Parent.Humanoid.WalkSpeed = ws
else
task.wait(2.7)
end
else
task.wait(2.7)
end
script.Parent.Sound:Stop()
targetAttach.Parent = script.Parent
targetAttach.Position = script.Parent.Position
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
end
end)
tool.Unequipped:Connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
if Heartbeat then Heartbeat:Disconnect() end
end)
tool.Equipped:Connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
if Heartbeat then Heartbeat:Disconnect() end
end)
local t = 0
game:GetService("RunService").RenderStepped:Connect(function()
t = t + 1
if t % 10 == 0 then
script.Parent.Beam0.TextureSpeed = math.random(1, 400)
script.Parent.Beam1.TextureSpeed = math.random(1, 400)
script.Parent.Beam2.TextureSpeed = math.random(1, 400)
elseif t % 10 == 1 then
script.Parent.Beam0.TextureSpeed = 0
script.Parent.Beam1.TextureSpeed = 0
script.Parent.Beam2.TextureSpeed = 0
end
if t > 9999 then t = 0 end
end)
Heartbeat:Connect and other events, they are RBXScriptSignal and these return an RBXScriptConnection when they connect, and it contains a method called Disconnect.
This happens because the attack is outside the loop, it should be inside.
local targetAttach = script.Parent:WaitForChild("CannonTarget")
local tool = script.Parent.Parent
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Heartbeat = nil
tool.Activated:Connect(function()
local obj = mouse.Target
if script.Parent.Beam0.Enabled == false then
targetAttach.Parent = game.Workspace.Terrain
script.Parent.Beam0.Enabled = true
script.Parent.Beam1.Enabled = true
script.Parent.Sound:Play()
local CanDamage = true
Heartbeat = game:GetService("RunService").Heartbeat:Connect(function()
targetAttach.Position = mouse.Hit.p
if not CanDamage then return end
CanDamage = false
if obj then
local Humanoid = obj.Parent:FindFirstChild("Humanoid")
if Humanoid then
print("hum")
local ws = Humanoid.WalkSpeed
Humanoid.WalkSpeed = 0
for i = 1, 10 do
Humanoid:TakeDamage(5)
task.wait(0.1)
end
Humanoid.WalkSpeed = ws
else
task.wait(2.7)
end
else
task.wait(2.7)
end
CanDamage = true
end)
script.Parent.Sound:Stop()
targetAttach.Parent = script.Parent
targetAttach.Position = script.Parent.Position
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
end
end)
tool.Unequipped:Connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
if Heartbeat then Heartbeat:Disconnect() end
end)
tool.Equipped:Connect(function()
script.Parent.Beam0.Enabled = false
script.Parent.Beam1.Enabled = false
script.Parent.Sound:Stop()
if Heartbeat then Heartbeat:Disconnect() end
end)
local t = 0
game:GetService("RunService").RenderStepped:Connect(function()
t = t + 1
if t % 10 == 0 then
script.Parent.Beam0.TextureSpeed = math.random(1, 400)
script.Parent.Beam1.TextureSpeed = math.random(1, 400)
script.Parent.Beam2.TextureSpeed = math.random(1, 400)
elseif t % 10 == 1 then
script.Parent.Beam0.TextureSpeed = 0
script.Parent.Beam1.TextureSpeed = 0
script.Parent.Beam2.TextureSpeed = 0
end
if t > 9999 then t = 0 end
end)