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

Script Running Faster Each Time Pressed?

Asked by
pilotly 15
4 years ago

Hello im having a issue with one of my scripts.

Every time it runs it runs a thouthand times. Execpt it keeps getting quicker.

-- Code in question

    runService.Heartbeat:Connect(function()
        for index, players in pairs(players:GetChildren())do
            local turnTime = players.Turn
            local name = players.Name


            if not turnTable[name] then
                if turnTime.Value > maxTime then
                    turnTime.Value = maxTime
                elseif turnTime.Value < maxTime then
                    turnTime.Value = turnTime.Value + regen

                end
            else 
                if turnTime.Value >= cost then
                    turnTime.Value = turnTime.Value - cost

                else
                    turnTable[name] = nil
                end

            end
        end
    end)

-- full code

local replicatedStorge = game:GetService("ReplicatedStorage")

local runService = game:GetService("RunService")


local players = game:GetService("Players")
local turnpart = game.Workspace.TurnTo


local maxTime = 500
local regen = 1
local cost = 1


local turnTable = {}



players.PlayerAdded:Connect(function(player)
    local turnTime = Instance.new("IntValue",player)
    turnTime.Value = 500
    turnTime.Name = "Turn"

    turnTime.Changed:Connect(function(property)
        replicatedStorge.Remotes.ResetUpdate:FireClient(player,turnTime.Value, maxTime)
        print("firingf21")
    end)
end)


replicatedStorge.Remotes.LowerBar.OnServerEvent:Connect(function(player,state,seat)
    local character = player.Character
    local humanoid = character:FindFirstChild("Humanoid")
    local root = humanoid:FindFirstChild("HumanoidRootPart")
    local car = seat.Parent
    local gyro = seat.BodyGyro
    local turn = false
    local turnTime2 = player.Turn

    if state == "Begin" then
        turnTable[player.Name] = true
    elseif state == "Ended" and turnTable[player.Name] then
        turnTable[player.Name] = nil

    end


    runService.Heartbeat:Connect(function()
        for index, players in pairs(players:GetChildren())do
            local turnTime = players.Turn
            local name = players.Name


            if not turnTable[name] then
                if turnTime.Value > maxTime then
                    turnTime.Value = maxTime
                elseif turnTime.Value < maxTime then
                    turnTime.Value = turnTime.Value + regen

                end
            else 
                if turnTime.Value >= cost then
                    turnTime.Value = turnTime.Value - cost

                else
                    turnTable[name] = nil
                end

            end
        end
    end)

1 answer

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
4 years ago

The issue with your code is simple: You have a remote event nested inside of another remote event.

Each time replicatedStorge.Remotes.LowerBar.OnServerEvent:Connect is called, it starts a new listener for runService.Heartbeat:Connect. This inturn makes your code execute more.

Solution: (Take the event in question and un-nest it)

local replicatedStorge = game:GetService("ReplicatedStorage")

local runService = game:GetService("RunService")


local players = game:GetService("Players")
local turnpart = game.Workspace.TurnTo


local maxTime = 500
local regen = 1
local cost = 1


local turnTable = {}



players.PlayerAdded:Connect(function(player)
    local turnTime = Instance.new("IntValue",player)
    turnTime.Value = 500
    turnTime.Name = "Turn"

    turnTime.Changed:Connect(function(property)
        replicatedStorge.Remotes.ResetUpdate:FireClient(player,turnTime.Value, maxTime)
        print("firingf21")
    end)
end)


replicatedStorge.Remotes.LowerBar.OnServerEvent:Connect(function(player,state,seat)
    local character = player.Character
    local humanoid = character:FindFirstChild("Humanoid")
    local root = humanoid:FindFirstChild("HumanoidRootPart")
    local car = seat.Parent
    local gyro = seat.BodyGyro
    local turn = false
    local turnTime2 = player.Turn

    if state == "Begin" then
        turnTable[player.Name] = true
    elseif state == "Ended" and turnTable[player.Name] then
        turnTable[player.Name] = nil

    end
end)

runService.Heartbeat:Connect(function()
    for index, players in pairs(players:GetChildren())do
        local turnTime = players.Turn
        local name = players.Name


        if not turnTable[name] then
            if turnTime.Value > maxTime then
                turnTime.Value = maxTime
            elseif turnTime.Value < maxTime then
                turnTime.Value = turnTime.Value + regen

            end
        else 
            if turnTime.Value >= cost then
                turnTime.Value = turnTime.Value - cost

            else
                turnTable[name] = nil
            end

        end
    end
end)
0
Perfect Solution. Plus learn a lesson! Thanks pilotly 15 — 4y
Ad

Answer this question