This script is making my Laptop slow!

Hello guys, I am having a problem, so I asked someone to help me with a script, so he gave me the script, but the problem is that it is lagging my laptop,

my laptop is trash because it has intel Celeron, so it is trash but when I run this code it stops my studio. it says that studio is not responding so can you look at the code properly and if it is any issue with script or just my laptop might be trash because it got updated yesterday So it might be the case!

local playerService = game:GetService("Players")
local button = script.Parent.redbutton

local function onHumanoidTouched(humanoid)
	
end

--button.Touched:Connect(function(hit)
	--local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	
	--if humanoid then
		--onHumanoidTouched(humanoid)
	--end
--end)




local isTouching = false

local function loopGive(hit)
	while isTouching do

		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if player then

			local leaderstats = player:FindFirstChild("leaderstats")

			if leaderstats then
				local cash = leaderstats:FindFirstChild("Cash")

				if cash then
					local pass2x = player.ButtonsFolder.Cash2x.Value
					local pass3x = player.ButtonsFolder.Cash3x.Value
					local pass5x = player.ButtonsFolder.Cash5x.Value
					if player:IsDescendantOf(button) then 
						print("true")
					else
						print("false") 
					end -- change this 
					while player:IsDescendantOf(button) do
						print("done5")
						cash.Value = cash.Value + 1 -- add $1 continuously
						wait(1)
					end
				end
			end
		end
	end
end

button.Touched:Connect(function(hit)
	isTouching = true
	loopGive(hit)
end)
button.TouchEnded:Connect(function(hit)
	isTouching = false
end)

If this isn’t true it will never hit that wait, so it run through the function at light speed a million times = lagging you out.

					end
				end
			end
		end
	end task.wait(0.33) <-- whatever # you need here.
end

or

button.Touched:Connect(function(hit)
	isTouching = true
	loopGive(hit)
    task.wait.(0.33)
end)
1 Like

Yeah, so how can I avoid it? and btw there is button that makes you 1$ per touch but the problem is that when the player is touching it will stop in just 3-4 seconds even though the player is still on the button? how can I fix it?

Easy … slow it down with a wait command. Probably running that function 10,000 times as fast as possible.

but it does not work, I want that when a player is above the button it should work but it only works for 3-4 second! I want it forever until the player has left the button!

local function loopGive(hit)
	while isTouching do

    end
end

Without anything being true here this is basically what you’re doing. This will always lag. Might even lock the program up.

so isTouching true or false? because in the script it will change to true

You need a pause … as I’ve shown you. If nothing is true.

oh so we need to slow it down right?

1 Like

Don’t underestimate how fast your computer is.

hey what if I write another digit than 0.33?

I personally like 0.33 – it’s as fast/slow as a screen wipe. Try whatever works.
This is as fast as I will ever use a wait statement. wait() ← is crazyness.

I don’t think it is fast compared to today’s laptop it is not that much fast, this laptop is only for browsing web but i installed a heavy game engine! :neutral_face:

from looking at your code i can tell that you have no idea what youre doing, heres a easier way to do this using magnitude

local Players = game:GetService("Players")
local Button = script.Parent.redButton

local ActivePlayers = {}

task.spawn(function()
	while true and task.wait(1) do 
		for i,v in pairs(Players:GetChildren()) do 
			if (v.Character) and (v.Character:FindFirstChild("HumanoidRootPart")) then 
				local Root = v.Character.HumanoidRootPart 

				if (Root.Position - Button.Position).Magnitude <= 6 and (not table.find(ActivePlayers, v.Name)) then 
					table.insert(ActivePlayers, v.Name)
				elseif (Root.Position - Button.Position).Magnitude > 6 and (table.find(ActivePlayers, v.Name)) then 
					table.remove(ActivePlayers, table.find(ActivePlayers, v.Name))
				end
			end
			if (table.find(ActivePlayers, v.Name)) then 
				local Coins = v.leaderstats.Cash
				Coins.Value += 1
			end
		end
	end
end)

You would be shocked how fast you really are … keep in mind the sever is involved with this.
If you send the flow into a: while true do end you’ll lock up … you’re doing about the same thing here.

Uhm I am beginner at scripting anyways can you explain what this code does?

No matter what happens that script is doing this … That is the key to it not locking up.
Very nice little add czo. Sweet and simple.

local Players = game:GetService("Players")
local Button = script.Parent.redButton

local ActivePlayers = {}
--// ^ table for storing players that are near the button.

task.spawn(function()
	while true and task.wait(1) do 
		for i,v in pairs(Players:GetChildren()) do 
			--// ^ loop trough all the players in game
			if (v.Character) and (v.Character:FindFirstChild("HumanoidRootPart")) then
				--// ^ check if their character and humanoidrootpart exists so we can check how far away from the button they are.
				local Root = v.Character.HumanoidRootPart 

				if (Root.Position - Button.Position).Magnitude <= 6 and (not table.find(ActivePlayers, v.Name)) then 
					--// ^ magnitude is basically how far away the 2 positions are apart, so we are checking if the humanoidrootpart is less than 6 studs away from the button, we are also checking if the player is not already in the ActivePlayers.
					table.insert(ActivePlayers, v.Name)
					--// ^ add the player to the ActivePlayers table
				elseif (Root.Position - Button.Position).Magnitude > 6 and (table.find(ActivePlayers, v.Name)) then 
					--// ^ if the if statement above doenst run then we check if the player is more than 6 studs away from the button and if they are in the ActivePlayers table.
					table.remove(ActivePlayers, table.find(ActivePlayers, v.Name))
					--// ^ if they are far away and in the table then we just remove them.
				end
			end
			if (table.find(ActivePlayers, v.Name)) then 
				--// ^ if the player is in the table then we add cash their value.
				local Coins = v.leaderstats.Cash
				Coins.Value += 1
			end
		end
	end
end)

Does this work better than touched event?

@CZXPEK
@2112Jay
Hey Thanks it worked!! I was trying to do this problem for a while thanks to both of you!!!

2 Likes