Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[SOLVED MYSELF]How to make a player's screen go white for a bit and then fade away without lagging?

Asked by 5 years ago
Edited 5 years ago

[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)
0
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" User#5423 17 — 5y
0
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. arthurdaniel91 72 — 5y
0
The size of the GUI will have little impact on performance? User#5423 17 — 5y
0
I think you are missing something here. Try and replicate the "lag" in a blank project. User#5423 17 — 5y
View all comments (8 more)
0
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 arthurdaniel91 72 — 5y
0
One big problemis that you are trying to change the gui fom the server so yes it will "lag" User#5423 17 — 5y
0
And you have no debounce for your castEquipmentFunction meaning it will also run multiple times. User#5423 17 — 5y
0
Yeah that might be it, thanks, I will try to make the GUI client-sided now arthurdaniel91 72 — 5y
0
Actually, there is a debounce at line 49 that (return end) thing works as a debounce. arthurdaniel91 72 — 5y
0
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. User#5423 17 — 5y
0
Depends, my style of scripting might be way different from other developers and a bit hard to crack. arthurdaniel91 72 — 5y
0
"When you get hit by a survivor" did you connect a Touched event to a part? Show the code for it. awesomeipod 607 — 5y

2 answers

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

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

Ad
Log in to vote
0
Answered by 5 years ago

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.

Answer this question