Certain Part of script works only half the time

I am making an anime - styled vfx, like Kameahmeah.

For some reason, about half the times, the second part of the vfx, the release, doesn’t appear, or appear but disappear half way with the tween.

I thought it might be the Instance:Clone() or the TweenService, but I tested them and they both seem fine. I have tried anything I can think of, and I truly don`t know what the cause of the problem is.

This is the local script, located inside StarterPack.

local player = game:GetService('Players').LocalPlayer

local rs = game:GetService('ReplicatedStorage')
local Remote = rs.MovesRemotes:WaitForChild(script.Name)
local TweenService = game:GetService('TweenService')
local Debris = game:GetService('Debris')

local UIS = game:GetService('UserInputService')

local damage = 15
local DamageDebounce = {}
local active = 0
local MaxPortions = 2
local cooldown = 2

character = player.Character

if not character or not character.Parent then character = player.CharacterAdded:wait() end

UIS.InputBegan:Connect(function(key,isTyping)
	if isTyping then
		return
	elseif key.KeyCode == Enum.KeyCode.E then 
		if active == 0 then
			active = 1
			Remote:FireServer(active, MaxPortions, damage)
		end
	end
end)

UIS.InputEnded:Connect(function(key,isTyping)
	if isTyping then
		return
	elseif key.KeyCode == Enum.KeyCode.E then
		if active == 1 then
			active = 2
			Remote:FireServer(active, MaxPortions, damage)
		end
	end
end)

local Meshes = Remote:WaitForChild("Meshes")
local InnerA = Meshes:WaitForChild("Inner")
local OutterA = Meshes:WaitForChild("Outter")
local beamWA = Meshes:WaitForChild("beamW")
local beamBA = Meshes:WaitForChild("beamB")
local WaveA = Meshes:WaitForChild("Wave")
local GroundA = Meshes:WaitForChild("Ground")

Remote.OnClientEvent:Connect(function(vfx_Player, vfx_Portion, vfx_MaxPortions, vfx_Damage)
	if vfx_Portion == 1 then
		local char = vfx_Player.Character
		local Humanoid = char:WaitForChild("Humanoid")
		char.PrimaryPart.Anchored = true
		--Hold
		local Folder = Instance.new('Folder', game.Workspace)
		Folder.Name = vfx_Player.Name .. " Kamehameha"
		
		local Hold = Humanoid:LoadAnimation(Remote.Animations.Hold)
		Hold:Play()

		local Inner = InnerA:Clone()
		Inner.Parent = Folder
		Inner.CFrame = char:WaitForChild('LeftHand').CFrame

		local InWeld = Instance.new('ManualWeld',Inner)
		InWeld.Name = "Weld"
		InWeld.Part0 = Inner
		InWeld.Part1 = char:WaitForChild("LeftHand")
		InWeld.C0 = Inner.CFrame:inverse() * char:WaitForChild('LeftHand').CFrame

		local Outter = OutterA:Clone()
		Outter.Parent = Folder
		Outter.CFrame = Inner.CFrame

		local OutWeld = Instance.new('ManualWeld')	
		OutWeld.Parent = Outter
		OutWeld.Name = "Weld"
		OutWeld.Part0 = Outter
		OutWeld.Part1 = Inner
		OutWeld.C0 = Outter.CFrame:inverse() * Inner.CFrame
	elseif vfx_Portion == 2 then
		local char = vfx_Player.Character
		local Humanoid = char:WaitForChild("Humanoid")
		local Hold = Humanoid:LoadAnimation(Remote.Animations.Hold)
		--Release
		Hold:Stop()
		local Folder = game.Workspace:WaitForChild(vfx_Player.Name .. " Kamehameha")
		local Release = Humanoid:LoadAnimation(Remote.Animations.Release)
		Release:Play()

		wait(.75)

		spawn(function()
			local TweenService = game.TweenService
			local Humanoid = game.Players.LocalPlayer.Character.Humanoid
			spawn(function()
				local tweened
				local info = TweenInfo.new(.05)
				for i=1,50,1 do
					local property
					if i ~= 50 then
						property = {CameraOffset = Vector3.new(math.random(-100,100)/100,math.random(-100,100)/100,0)}
					else
						property = {CameraOffset = Vector3.new(0,0,0)}
					end
					tweened = TweenService:Create(Humanoid,info,property)

					tweened:Play()
					tweened.Completed:Wait(math.huge)
				end
			end)
		end)
		
		local Inner = InnerA:Clone()
		Inner.Size = Vector3.new(8,8,8)
		Inner.Anchored = true
		Inner.CFrame = char:WaitForChild('HumanoidRootPart').CFrame
		print(Inner.Parent)
		Inner.Parent = Folder

		local Outter = OutterA:Clone()
		Outter.Parent = Folder
		Outter.Size = Vector3.new(8.25,8.25,8.25)
		Outter.Anchored = true
		Outter.CFrame = Inner.CFrame

		local Wave = WaveA:Clone()
		Wave.Parent = Folder
		Wave.CFrame = Inner.CFrame * CFrame.new(0,0,2)
		Wave.Orientation += Vector3.new(0,90,90)

		spawn(function()
			while wait() do
				Wave.Orientation += Vector3.new(60,0,0)
			end
		end)

		local beamW = beamWA:Clone()
		beamW.Parent = Folder
		beamW.CFrame = Inner.CFrame * CFrame.new(0,0,-3)
		beamW.Orientation += Vector3.new(0,90,0)

		local beamB = beamBA:Clone()
		beamB.Parent = Folder
		beamB.CFrame = Inner.CFrame * CFrame.new(0,0,-3)
		beamB.Orientation += Vector3.new(0,90,0)

		spawn(function()
			local PartFolder = Instance.new('Folder', Folder)
			PartFolder.Name = "PartFolder"
			for i = 1,100,1 do
				local rayParams = RaycastParams.new()
				rayParams.FilterType = Enum.RaycastFilterType.Blacklist
				rayParams.FilterDescendantsInstances = char:GetChildren()
				local raycastResult = game.Workspace:Raycast(char.Head.Position, (char.HumanoidRootPart.Position - char.Head.Position).Unit * 10, rayParams)

				if raycastResult.Instance.Name ~= "Terrain" then
					local rand = math.random(10,30) / 10
					local Part = GroundA:Clone()
					Part.Parent = PartFolder
					Part.Material = raycastResult.Material
					Part.Color = raycastResult.Instance.Color
					Part.Size = Vector3.new(rand,rand,rand)
					Part.Position = char.RightLowerLeg.Position - Vector3.new(0,1,0) + char.HumanoidRootPart.CFrame.LookVector.Unit * (0.9 * i + 10) + Vector3.new(1,0, -char.HumanoidRootPart.CFrame.LookVector.X/ char.HumanoidRootPart.CFrame.LookVector.Z).Unit * math.random(-20,20)/10
					Part.Orientation = Vector3.new(math.random(1,360),math.random(1,360),math.random(1,360))
					
					spawn(function()
						wait(5)
						for i = 1,25,1 do
							Part.Position -= Vector3.new(0,.2,0)
							wait()
						end
						Part:Destroy()
					end)
				end
			end
			wait(5 + (wait() * 24))
			Folder:Destroy()
		end)

		beamW.Touched:Connect(function(hit)
			if not hit:IsDescendantOf(char) and hit:FindFirstAncestorOfClass("Model") then
				if hit:IsA('Part') or hit:IsA('MeshPart') then
					local EHumanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
					if EHumanoid and not table.find(DamageDebounce, hit:FindFirstAncestorOfClass("Model")) then
						table.insert(DamageDebounce, hit:FindFirstAncestorOfClass("Model"))
						EHumanoid:TakeDamage(vfx_Damage)
						wait(1.6)
						table.remove(DamageDebounce, table.find(DamageDebounce, hit:FindFirstAncestorOfClass("Model")))
					end
				end
			end
		end)

		for _,v in pairs({beamW,beamB}) do
			local goal = {}
			goal.Size = v.Size + Vector3.new(100,0,0)
			goal.CFrame = v.CFrame * CFrame.new(50,0,0)

			local info = TweenInfo.new(.35,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
			local tween = TweenService:Create(v,info,goal)
			tween:Play()
		end

		wait(1.5)

		for i, v in pairs(Folder:GetChildren()) do
			if v.Name ~= "PartFolder" then
				Debris:AddItem(v,1.5)
				local goal = {}
				goal.Transparency = 1

				local info = TweenInfo.new(1.5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
				local tween = TweenService:Create(v,info,goal)
				tween:Play()
			end
		end
		wait(1.5)
		char.PrimaryPart.Anchored = false

		wait(cooldown)
		
		if player == vfx_Player then
			active = 0
		end
	end
end)

This is the global script, located inside of ServerScriptService.

local ReplicaedStorage = game:GetService('ReplicatedStorage')
local debounce = {}
local globalDebouncer = false

for _, Remote in pairs(ReplicaedStorage.MovesRemotes:GetChildren()) do
	Remote.OnServerEvent:Connect(function(player, Portion, MaxPortions, Damage)
		if not globalDebouncer then
			print(Portion, "/", MaxPortions, " Global")
			globalDebouncer = true
			if Portion == MaxPortions then
				print(Portion, "/", MaxPortions)
				Remote:FireAllClients(player, Portion, MaxPortions, Damage)
				if table.find(debounce, player.Name) then 
					table.remove(debounce,table.find(debounce,player.Name)) 
				end
			elseif Portion == 1 then
				print(Portion, "/", MaxPortions)
				table.insert(debounce, player.Name)
				Remote:FireAllClients(player, Portion, MaxPortions, Damage)
			else
				print(Portion, "/", MaxPortions)
				Remote:FireAllClients(player, Portion, MaxPortions, Damage)
			end
			globalDebouncer = false
		end
	end)
end

Thanks in advance. I am truly lost.

1 Like

I have few questions:

  1. Does it happen regularly (an example - every x times during a test play it doesn’t work)?
  2. When it doesn’t work, do any of the two scripts print error(s) (probably not because you used printing to print information and that’s how you could see also potential errors)
  3. local Remote = rs.MovesRemotes:WaitForChild(script.Name)
    What is script.Name? A name created by you and other script(s) don’t create this name?
  4. Could you provide something visual showing the result of the scripts working properly and the part of the script not working properly (an example - screenshots, MS paint picture)?
  5. Which exacly part of your code sometimes doesn’t work?

No errors, in total it happens about once every three times. script.Name is Kamehameha. I am still not sure what caused the problem but it was just fixed. I appreciate your willingness to help.

Add to lines 1-5 this code in the local script and then check how many time the code doesn’t work properly (I expect the same results, but why not to try this code?):

print("Loading the game...")
if not game:IsLoaded() then
game.Loaded:Wait()
end
print("The game is loaded")

If your result is the same: I have an idea of making two separate local scripts for each vfx. After doing that, try making your code as clean as you can. The second vfx might still not work but in this step you should have less code to investigate. As a final step you can keep the two scripts or combine them into one local script.