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