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

Why when the player touch the brick its lagging like crazy?

Asked by
Simnico99 206 Moderation Voter
7 years ago
Edited 7 years ago

when the player touch a part its starting from 60fps then drop to 10fps


local debounce = false local debounce2 = false local Stop = false for i, v in pairs(script.Parent:GetChildren()) do if v.ClassName == "Part" then v.Touched:connect(function(otherPart) if debounce == false then print("God damn") debounce = true local hum = otherPart.Parent:FindFirstChild("Humanoid") if hum then local Player = game.Players:FindFirstChild(hum.Parent.Name) game.ReplicatedStorage.OutBound:FireClient(Player, true) Stop = false if script:FindFirstChild(Player.Name) then else local Pl = Instance.new("StringValue", script) Pl.Name = Player.Name repeat wait(1) hum.Health = hum.Health - hum.MaxHealth / 100 until Stop == true wait(2) debounce = false end end end end) v.TouchEnded:connect(function(otherPart) if debounce2 == false then debounce2 = true local hum = otherPart.Parent:FindFirstChild("Humanoid") if hum then Stop = true print("Oh god") if script:FindFirstChild(hum.Parent.Name):Destroy() then script:FindFirstChild(hum.Parent.Name):Destroy() local Player = game.Players:FindFirstChild(hum.Parent.Name) game.ReplicatedStorage.OutBound:FireClient(Player, false) end wait(2) debounce2 = false end end end) end end

1 answer

Log in to vote
1
Answered by
tkcmdr 341 Moderation Voter
7 years ago
Edited 7 years ago

Hey Simnico99,

I hate to tell you this, but your script is incredibly messy both in writing and in implementation. A much better way to approach this problem is as follows:

local OutBound          = game.ReplicatedStorage:WaitForChild("OutBound")
local HumanoidsInBounds = {}    -- A list of humanoids in bounds
local LastUpdate            = 0 -- The last time an update was done

for k, child in pairs(script.Parent:GetChildren()) do
    if (child:IsA("Part")) then
        child.Touched:Connect(function(other)
            local humanoid  = other.Parent:FindFirstChild("Humanoid")
            local player        = game.Players:GetPlayerFromCharacter(humanoid.Parent)

            if (humanoid and player) then
                -- Record the humanoid found
                HumanoidInBounds[player] = humanoid

                OutBound:FireClient(player, true)
            end
        end)

        child.TouchEnded:Connect(function(other)
            local humanoid  = other.Parent:FindFirstChild("Humanoid")
            local player        = game.Players:GetPlayerFromCharacter(humanoid.Parent)

            -- Same as before, but we check to see if the humanoid is in HumanoidsInBounds or not
            if (humanoid and player and HumanoidsInBounds[player]) then
                HumanoidsInBounds[player] = nil

                OutBound:FireClient(player, false)
            end
        end)
    end
end

-- Runs every RunService.Stepped call, which takes place *about* 30 times every second.
local function Update()
    local currentTime = tick()

    -- If the current time is more than one second in the past
    if ((currentTime - LastUpdate) > 1) then
        -- Set LastUpdate to the current time
        LastUpdate = currentTime

        -- And damage each humanoid in HumanoidsInBounds
        for k, humanoid in pairs(HumanoidsInBounds) do
            if (humanoid) then
                -- Deplete 10% of the humanoid's max health
                humanoid:TakeDamage(humanoid.MaxHealth/10)
            end
        end
    end
end

game:GetService("RunService").Stepped:Connect(Update)

I hope this helps. Please, be more careful when programming and look for more... straightforward solutions to problems. You would really benefit from studying the Wiki more and reading NoahWillCode's Clean and Tidy. Best of luck with your project, Simnico99. Have a nice day.

Warm regards,

tkcmdr

Sources:

Table Page

Basic functions Page

Humanoid API Page

RunService API Page

Players API Page

Instance:IsA API Page

Ad

Answer this question