[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)
rather than cloning the gui to the players script, just make it a a starter gui and enable it when they get stunned and disable it when the transparency = 1. All GUIs have a "Enabled" property
As I was checking the script, I realized what the error was.
On line 49 it checks if the target is already stunned, if he is then the function won't execute any further.
And then on the stun function, I forgot to make it so that it adds the value "IsHitByEquipment", that is what was causing the lag.
Thanks kingdom5 I just investigated what you thought was and you were right.