You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I want to give 1 money who afk on the area per 10 secound
What is the issue? the part is adding value so many.
while true do
wait(10)
script.Parent.Touched:Connect(function(Player)
local humanoid = Player.Parent:FindFirstChild("Humanoid")
if humanoid then
local Plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
print(Plr)
local leaderstats = Plr:FindFirstChild("leaderstats")
local Money = leaderstats:GetChildren()
Money[1].Value = Money[1].Value + 1
end
end)
end
I mean you are making a new touched event every 10 seconds so uh yea… thats gonna casue some issues. Because .Touched doesn’t fire actively to who is touching it, but rather fires a lot, so you need a debounce. Instead do something like…
while true do
task.wait(10)
local GaveMoney = {}
for i, v in script.Parent:GetTouchingParts() do
if game.Players:GetPlayerFromCharacter(v.Parent) and table.find(GaveMoney, game.Players:GetPlayerFromCharacter(v.Parent).Name) == nil then
table.insert(GaveMoney, game.Players:GetPlayerFromCharacter(v.Parent).Name)
local Plr = game.Players:GetPlayerFromCharacter(v.Parent)
print(Plr)
local leaderstats = Plr:FindFirstChild("leaderstats")
local Money = leaderstats:GetChildren()
Money[1].Value = Money[1].Value + 1
end
end
table.clear(GaveMoney)
end
first of all, you’re making a touched event in a while loop. If you still want to do this, you should instead use script.Parent.Touched:Once(function(Player), which will automatically disconnect it when triggered, but I reccommend just using a touched statement with a debounce if you want a better-ish system
Question is, do you want to give money every 10 seconds relative to the start of the server, or every 10 seconds relative to when each player joins the area? Here are ways to implement each. As a note, the relative to player option requires the use of a coroutine to keep the thread running.
10 seconds relative to server:
llocal playersInArea = {}
script.Parent.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(character)
if player then
playersInArea[player.UserId] = true
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
playersInArea[player.UserId] = nil
end
end)
while true do
task.wait(10)
for userId, _ in pairs(playersInArea) do
local player = game.Players:GetPlayerByUserId(userId) -- Get the player object using the user ID
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local Money = leaderstats:FindFirstChild("Money")
if Money then
Money.Value = Money.Value + 1
end
end
end
end
end
10 seconds relative to each player:
local playersInArea = {}
local function addMoney(player)
while playersInArea[player.UserId] do
task.wait(10)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local Money = leaderstats:FindFirstChild("Money")
if Money then
Money.Value = Money.Value + 1
end
end
end
end
script.Parent.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(character)
if player then
if not playersInArea[player.UserId] then
playersInArea[player.UserId] = true
coroutine.wrap(addMoney)(player)
end
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(character)
if player then
playersInArea[player.UserId] = nil
end
end
end)
If you have any questions about how each script works or more details, let me know!
Yup, its the check if the player is not already in the table inside of touch connect that acts as the initializer the first connect, then serves as a barrier the next connections like a debounce.