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

01runService.Heartbeat:Connect(function()
02    for index, players in pairs(players:GetChildren())do
03        local turnTime = players.Turn
04        local name = players.Name
05 
06 
07        if not turnTable[name] then
08            if turnTime.Value > maxTime then
09                turnTime.Value = maxTime
10            elseif turnTime.Value < maxTime then
11                turnTime.Value = turnTime.Value + regen
12 
13            end
14        else
15            if turnTime.Value >= cost then
View all 24 lines...

-- full code

01local replicatedStorge = game:GetService("ReplicatedStorage")
02 
03local runService = game:GetService("RunService")
04 
05 
06local players = game:GetService("Players")
07local turnpart = game.Workspace.TurnTo
08 
09 
10local maxTime = 500
11local regen = 1
12local cost = 1
13 
14 
15local turnTable = {}
View all 71 lines...

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)

01local replicatedStorge = game:GetService("ReplicatedStorage")
02 
03local runService = game:GetService("RunService")
04 
05 
06local players = game:GetService("Players")
07local turnpart = game.Workspace.TurnTo
08 
09 
10local maxTime = 500
11local regen = 1
12local cost = 1
13 
14 
15local turnTable = {}
View all 71 lines...
0
Perfect Solution. Plus learn a lesson! Thanks pilotly 15 — 4y
Ad

Answer this question