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)
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?
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!
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!
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.
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)