Script doesnt work and cant stop firing

image

I wanted to do a multiple projectile attack that i could change their direction by simply holding e, however this doesn’t work, i just got back to scripting and i got no clue on what to do. i used runservice for updating the cframe live.

Local Script

local player = game.Players.LocalPlayer
local char = player.Character
local rs = game:GetService("ReplicatedStorage")
local re = rs.RE
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")
local db = true
local unitray = mouse.UnitRay
local mhitp = mouse.Hit.Position
local runs = game:GetService("RunService")
local playerGui = player:WaitForChild("PlayerGui")

local function iskeydown()
	return uis:IsKeyDown(Enum.KeyCode.E)
end
uis.InputBegan:Connect(function(input, gameProcessed, isTyping)
	if input.KeyCode == Enum.KeyCode.E then
		if gameProcessed or isTyping then return end
		rs.RE:FireServer(mhitp, nil, nil)
		re.OnClientEvent:Connect(function()
			if not iskeydown() then
				re:FireServer(mhitp, unitray, false)
			else
				runs.Heartbeat:Connect(function()
					rs.RE:FireServer(mhitp, unitray, true)
				end)
			end
		end)
	end
end)

Server Script

local rs = game:GetService("ReplicatedStorage")
local re = rs.RE
local ts = game:GetService("TweenService")

local dmg = 10
local debris = 30

local MaterialRandomizationTable = {
	Enum.Material.Cobblestone;
	Enum.Material.Brick;
	Enum.Material.Wood;
	Enum.Material.Pebble;
	Enum.Material.Asphalt;
	Enum.Material.Basalt;
	Enum.Material.Concrete;
}

re.OnServerEvent:Connect(function(plr, mhitp, input, unitray, condition)
	print("hi1")
	local char = plr.Character
	local lt = char.LowerTorso
	local projectile
	if unitray == nil and condition == nil then
		for i = 1,debris do
			print("hi2")
			projectile = rs.SamplePart:Clone()
			projectile.Position = lt.Position + Vector3.new(math.random(-40,40), -40, math.random(-50,50))
			projectile.Material = MaterialRandomizationTable[math.random(1,#MaterialRandomizationTable)]
			projectile.Parent = workspace.Destructibles
			
			local bricktween = ts:Create(projectile, TweenInfo.new(1.75,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0), {Position = lt.Position + Vector3.new(math.random(-40,40), math.random(2,20), math.random(-50,50))})
			bricktween:Play()
			
			if i == debris then
				bricktween.Completed:Connect(function()
					re:FireClient(plr)					
				end)
			end
		end
	elseif condition == true then
		print("hi3")
		for i = 1,debris do
			projectile.CFrame = CFrame.new(projectile.CFrame.Position,mhitp)
		end
	elseif condition == false then
		print("hi4")
		projectile.CFrame = CFrame.new(projectile.CFrame.Position,mhitp)
		
		local velocitycreation = coroutine.create(function()
			if projectile.Parent == workspace.CurrentDestructibles then return end
			wait(1.75)
			local velocity = Instance.new("BodyVelocity")
			projectile.Anchored = false
			velocity.Parent = projectile
			velocity.MaxForce = Vector3.new(9e6,9e6,9e6)
			velocity.Velocity = projectile.CFrame.LookVector * 70
			velocity.Name = "BodyVelocity"
			debris:AddItem(projectile, 8)
			print('launched2')
		end)
		
		coroutine.resume(velocitycreation)
		
		projectile.Touched:Connect(function(hit)
			if projectile.Parent == workspace.CurrentDestructibles then return end
			if hit:IsDescendantOf(plr.Character) then return end
			if hit.Parent:FindFirstChild('Humanoid') then
				local ehum = hit.Parent:FindFirstChild("Humanoid")
				if ehum:FindFirstChild(projectile.Name) then return end
				local dval = Instance.new('BoolValue', ehum)
				dval.Name = projectile.Name
				ehum:TakeDamage(dmg)
			end
		end)
	end
end)
1 Like

Probably because you are connecting Heartbeat everytime you press the button.

Not totally sure, but using heartbeat while pressing E will multiply a lot. Maybe try adding a debounce.