Dodgeball Charging Spam

Hello, this code is supposed to charge the ball when you hold down the right mouse and resets the charge when you release I thought it was working but when you repeatedly stop and start the charge the next time you charge the ball is prints in the console very fast and charges the ball almost instantly. The code isn’t finished and a little messy but I would appreciate any help.

local ThrowBallRm = game.ReplicatedStorage.BallRemoteEvents.ThrowBall


ThrowBallRm.OnServerEvent:Connect(function(plr, mousePosition)

	local rayBehavior = fastcast.newBehavior()

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {plr.Character:GetDescendants()}

	rayBehavior.RaycastParams = params
	rayBehavior.Acceleration = Vector3.new(0, -50, 0)
	rayBehavior.AutoIgnoreContainer = true
	rayBehavior.CosmeticBulletTemplate = ball
	rayBehavior.CosmeticBulletContainer = workspace.Dodgeballs

	local dir = (mousePosition - plr.Character.Dodgeball.Position).Unit

	local activeCast = caster:Fire(plr.Character.Dodgeball.Position, dir, charge, rayBehavior)

	
	plr.Character.Dodgeball:Destroy()

	task.wait(1) -- Keep the wait to prevent immediate pickup
	plr.Character.HasBall.Value = false
end)

local chargeEvent = game.ReplicatedStorage.BallRemoteEvents.ChargeBall
local cancelEvent = game.ReplicatedStorage.BallRemoteEvents.CancelCharge

local cancel = false



local firstCharge = game.ReplicatedStorage.BallVFX.Fire1
local secondCharge = game.ReplicatedStorage.BallVFX.Fire2


local function enableVFX (vfxObject)
	for i, v in vfxObject:GetChildren() do
		if v:IsA("ParticleEmitter") then
			v.Enabled = true
		end
	end
end

local function disableVFX (vfxObject)
	for i, v in vfxObject:GetDescendants() do
		if v:IsA("ParticleEmitter") then
			v:Destroy()
		end
		
		if v.Name == "Fire1Weld" then
			v:Destroy()
		end
		
		if v.Name == "Fire2Weld" then
			v:Destroy()
		end
	end
end


chargeEvent.OnServerEvent:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 15
	local highlight = Instance.new("Highlight")
	highlight.Parent = plr.Character
	highlight.FillColor = Color3.new(1, 1, 1)
	highlight.OutlineColor = Color3.new(1, 1, 1)
	highlight.DepthMode = Enum.HighlightDepthMode.Occluded
	highlight.FillTransparency = 0.8
if charge < 200 then
	cancel = false
	repeat 
		print(charge)
		charge += 10
		task.wait(.5)
		if charge >= 200 then
			
		elseif charge == 155 then
				if not plr.Character:FindFirstChild("Fire2") then
					disableVFX(plr.Character.Fire1)
					local secondChargeClone = secondCharge:Clone()
					secondChargeClone.Parent = plr.Character
					secondChargeClone.Position = plr.Character.Dodgeball.Position
					local weld = Instance.new("Weld")
					weld.Name = "Fire2Weld"
					weld.Part0 = plr.Character.Dodgeball
					weld.Part1 = secondChargeClone
					weld.Parent = secondChargeClone
					enableVFX(secondChargeClone)
				end
		elseif charge == 105 then
		if not plr.Character:FindFirstChild("Fire1") then
			local firstChargeClone = firstCharge:Clone()
			firstChargeClone.Parent = plr.Character
			firstChargeClone.Position = plr.Character.Dodgeball.Position
			local weld = Instance.new("Weld")
			weld.Name = "Fire1Weld"
			weld.Part0 = plr.Character.Dodgeball
			weld.Part1 = firstChargeClone
			weld.Parent = firstChargeClone
			enableVFX(firstChargeClone)
		end
	end
	until charge >= 200 or cancel == true
	cancel = true
end
end)

local function findVFX(plr)
	for i, v in plr.Character:GetChildren() do
		if v.Name == "Fire1" then
			return v
		end
		
		if v.Name == "Fire2" then
			return v
		end
	end
end

cancelEvent.OnServerEvent:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 30
	cancel = true
	print("Cancled")
	local vfx = findVFX(plr)
	if vfx then
		disableVFX(vfx)
	end
	
	charge = 65
end)
1 Like

From a quick view at the code, it seems you need to simply add more checks and reset the ball’s charge progression in more areas that it may be lacking in, like after throwing the dodgeball for example.

1 Like