VFX shield does not delete if other players are activating the shield

I need a vfx shield to disapear when unblocking.

I made a vfx replicator which has a bug. My game is about sword fighting and there is a blocking option.

When you block, a vfx shield appears. It works as intended, although when multiple people block, it bugs out. Lets say that there is multiple people blocking. If one of them unblocks, then their shield will not disappear until everyone isn’t blocking. I’m not sure why this is happening.

I have 3 scripts in play. One is to receive events from clients to invoke all clients using vfxRemote:FireAllClients(player, ability). This goes to a local script which sends the information given to a module to actually play the vfx. The scripts are in order

-- Script that fires all client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remotes = ReplicatedStorage:WaitForChild("RemoteFolder")
local vfxRemote = remotes:WaitForChild("VfxRemote")
local modules = ReplicatedStorage:WaitForChild("Modules")
local vfxHandler = require(modules:WaitForChild("VFXHandler"))


vfxRemote.OnServerEvent:Connect(function(player, ability)
	vfxRemote:FireAllClients(player, ability)
end)
-- Script that sends information to the vfx module
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remotes = ReplicatedStorage:WaitForChild("RemoteFolder")
local vfxRemote = remotes:WaitForChild("VfxRemote")
local modules = ReplicatedStorage:WaitForChild("Modules")
local vfxHandler = require(modules:WaitForChild("VFXHandler"))


vfxRemote.OnClientEvent:Connect(function(player, ability)
	local char = player.Character or player.CharacterAdded:Wait()
	vfxHandler[ability](char)
end)
-- Vfx Module
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local vfx = ReplicatedStorage:WaitForChild("VFX")
local moduleFolder = ReplicatedStorage:WaitForChild("Modules")
local tweeningModule = require(moduleFolder:WaitForChild("TweeningModule"))
local vfxFolder = workspace:WaitForChild("VFXFolder")
local Players = game:GetService("Players")

local shield = vfx:WaitForChild("Shield")

local module = {
	["ShieldOn"] = function(char)
		local player = Players:GetPlayerFromCharacter(char)
		local charValues = char:FindFirstChild("CharValues")
		local torso = char:FindFirstChild("Torso")
		
		
		for i, vfx in vfxFolder:GetChildren() do
			if vfx.Name == "Shield" and vfx:GetAttribute("OwnedId") == player.UserId then return end
		end
		
		local shieldEffect = shield:Clone()
		local shieldSound = shieldEffect:WaitForChild("Sounds"):WaitForChild("ShieldSound")
		
		shieldEffect:SetAttribute("OwnedId", player.UserId)
		
		shieldSound:Play()
		local shieldMotor = Instance.new("Motor6D")
		shieldMotor.Name = "ShieldMotor"
		
		shieldMotor:SetAttribute("ItemMotor", true)

		shieldMotor.Part0 = shieldEffect
		shieldMotor.Part1 = torso

		shieldMotor.C1 = charValues["ShieldCFrame"].Value

		shieldMotor.Parent = shieldEffect
		shieldEffect.Transparency = 1
		shieldEffect.Parent = vfxFolder

		tweeningModule.Tween(shieldEffect, {Transparency = 0.5}, 1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0, false)
	end,
	
	["ShieldOff"] = function(char)
		local player = Players:GetPlayerFromCharacter(char)
		
		for i, vfxEffect in vfxFolder:GetChildren() do
			local shieldEffect = vfxFolder:FindFirstChild("Shield")

			if shieldEffect and shieldEffect:GetAttribute("OwnedId") == player.UserId then
				print("Destroyed", char.Name)
				shieldEffect:Destroy()
			end
		end
	end,
}

return module

1 Like

You’re using FireAllClients which obviously will fire to all clients and what you want to do is check each individual client if they have a shield on or not. You could just use FireClient or you could still use FireAllClients but check by individual player using something like:

vfxRemote.OnClientEvent:Connect(function(player, ability)
    if player == Players.LocalPlayer then
        vfxHandler[ability](player.Character)
    end
end)
2 Likes

i did that and now the vfx only apears to the person who’s using the shield

1 Like

I wasn’t able to get a good look at it before but I see the problem now.
In the shield off section you put local shieldEffect = vfxFolder:FindFirstChild("Shield) which means it will only find the first instance of a shield instead of iterating through all of them so as far as i can see you’ll just use the shieldEffect variable from before.

Thats why I set an attribute with the player Id so that it will find the exact shield.