[ALREADY SOLVED]
This is the third time I am asking this question since the last two times I asked I didn't get an answer.
I am making a stun system that when the player gets hit by a survivor with a baseball bat, he gets stunned and his screen turns white. It works perfectly, but I am having a big problem involving lag.
I am currently using a GUI with a blank frame with the size set to {0, 1000}, {0, 1000} and when the player gets stunned the GUI gets cloned to the player's PlayerGui, and the stunned player's FPS drops dramatically and I have no idea why.
I wanted to know if there is a way to fix the lag or if there is a different way to do it what doesn't lag.
Script just incase you need it
--<Variables>--
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local remotesFolder = repStorage:WaitForChild("Remotes")
local GUIsFolder = repStorage:WaitForChild("GUIs")
local equipmentRemote = remotesFolder:WaitForChild("EquipmentRemote")
local stunnedGUI = GUIsFolder:WaitForChild("StunnedGUI")
--<Functions>--
local function castEquipment(player)
local wPlayer = workspace:WaitForChild(player.Name)
if not wPlayer:FindFirstChild("Survivor") then return end
if wPlayer:FindFirstChild("CantUseEquipment") then return end
local humanoid = wPlayer:WaitForChild("Humanoid")
local equipment = wPlayer:WaitForChild("Equipment")
local equipmentAnimation = wPlayer:WaitForChild("EquipmentAnimation")
local hitSFX = equipment:WaitForChild("HitSFX")
local swingSFX = equipment:WaitForChild("SwingSFX")
swingSFX:Play()
local equipmentAnimationTrack = humanoid:LoadAnimation(equipmentAnimation)
equipmentAnimationTrack:Play()
local cantUseEquipment = Instance.new("BoolValue", wPlayer)
cantUseEquipment.Name = "CantUseEquipment"
local isUsingEquipment = Instance.new("BoolValue", wPlayer)
isUsingEquipment.Name = "IsUsingEquipment"
local stats = wPlayer:WaitForChild("Stats")
local equipmentSpeed = stats:WaitForChild("EquipmentSpeed")
local equipmentCoolDown = stats:WaitForChild("EquipmentCoolDown")
local equipmentFunction = stats:WaitForChild("EquipmentFunction")
debris:AddItem(cantUseEquipment, equipmentCoolDown.Value)
debris:AddItem(isUsingEquipment, equipmentSpeed.Value)
local function castEquipmentFunction(hit)
if not wPlayer:FindFirstChild("IsUsingEquipment") then return end
if hit.Parent:FindFirstChild("IsHitByEquipment") then return end
if hit.Parent:FindFirstChild("Survivor") then return end
if hit.Parent.Name == player.Name then return end
if string.match(hit.Parent.Name, "Dummy") then return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if equipmentFunction.Value == "Stun" then
humanoid.WalkSpeed = 0
local victimStats = hit.Parent:WaitForChild("Stats")
local victimDefaultWalkSpeed = victimStats:WaitForChild("DefaultWalkSpeed")
local sVictim = players:WaitForChild(player.Name)
local sVictimPlayerGUI = sVictim:WaitForChild("PlayerGui")
local newStunnedGUI = stunnedGUI:Clone()
stunnedGUI.Parent = sVictimPlayerGUI
wait(2)
for i = 1, 100 do
newStunnedGUI.BackgroundTransparency = newStunnedGUI.BackgroundTransparency + (.01)
wait(.01)
end
humanoid.WalkSpeed = victimDefaultWalkSpeed.Value
end
end
end
for i, v in pairs(wPlayer:GetChildren()) do
if v:IsA("Part") or v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
v.Touched:Connect(castEquipmentFunction)
end
end
end
--<Callers>--
equipmentRemote.OnServerEvent:Connect(castEquipment)

12 comments
You would use the tween service. The "lag" is probably down to another bit of code you are using. A simple fade will not cause the fps to drop. Use the micro profiler to check what is causing the "lag"
I honestly don't think that is the case since if I make the GUI smaller the FPS doesn't drop, it's the size of the GUI and I want to keep it's size.
The size of the GUI will have little impact on performance?
I think you are missing something here. Try and replicate the "lag" in a blank project.
When you get hit by a survivor, your walkspeed is set to 0 and the GUI is cloned, when the GUI is cloned, the FPS drops to 1
One big problemis that you are trying to change the gui fom the server so yes it will "lag"
And you have no debounce for your castEquipmentFunction meaning it will also run multiple times.
Yeah that might be it, thanks, I will try to make the GUI client-sided now
Actually, there is a debounce at line 49 that (return end) thing works as a debounce.
I am not sure it will work as you expect. but these are things only you can check. also remember that each time the remote runs you are connecting new Touched functions. Very very easy to exploit.
Depends, my style of scripting might be way different from other developers and a bit hard to crack.
"When you get hit by a survivor" did you connect a Touched event to a part? Show the code for it.