local part = script.Parent – The part this script is attached to
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
-- Check if the player has a 'Wins' leaderstats value
local Clicks = player.Data.PlayerData.Currency
if Clicks then
-- Increase the player's wins by 1 every 5 minutes
local function giveClicks()
Clicks.Value = Clicks.Value + 100
end
-- Check if there's already a timer running for this player
if not player:FindFirstChild("ClicksTimer") then
local timer = Instance.new("BoolValue")
timer.Name = "ClicksTimer"
timer.Parent = player
-- Give the first win immediately, then every 5 minutes
giveClicks()
while timer and timer.Parent do
task.wait(1) -- 5 minutes
giveClicks()
end
end
end
end
end
-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)
I dont get why your adding leaderstats when the player touches the part. If you want to access the leaderstats by touching the part just send a bindable events to the leaderstats script.
I have added a ui which shows the clicks. Also I have changed the script to this local part = script.Parent -- The part this script is attached to
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
-- Check if the player has a 'Wins' leaderstats value
local Clicks = player.Data.PlayerData.Currency
if Clicks then
-- Increase the player's wins by 1 every 5 minutes
local function giveClicks()
Clicks.Value = Clicks.Value + 100
end
-- Check if there's already a timer running for this player
if not player:FindFirstChild("ClicksTimer") then
local timer = Instance.new("BoolValue")
timer.Name = "ClicksTimer"
timer.Parent = player
-- Give the first win immediately, then every 5 minutes
giveClicks()
while timer and timer.Parent do
task.wait(1) -- 5 minutes
giveClicks()
end
end
end
end
end
-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)
just make a leaderstat script that adds this leaderstat and there is no need to make this section. It really does improve workflow and makes finding the issue easier.
Your just going to need this script to do the calculations then send the bindable event at the end of the function of the value that you want to add.
From your script I see that you want to add 100 Clicks to the Clicks leaderstat every 5 minutes. You can simplify this script to
local event = game:GetService("ServerStorage").Bindables.Event
local clicks = 0
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
while otherPart.Parent == player do
event:Fire(100)
task.wait(300)
end
end
end
The leaderstats script is going to receive this and add 100 clicks value that way
Ok I want when I touch the part it gives me 100 clicks per second and when I step off the part it stops giving me clicks.
Script:
local part = script.Parent -- The part this script is attached to
-- Function to handle the touch event
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
-- Check if the player has a 'Wins' leaderstats value
local Clicks = player.Data.PlayerData.Currency
if Clicks then
-- Increase the player's clicks by 100 every second
local function giveClicks()
Clicks.Value = Clicks.Value + 100
end
-- Check if there's already a timer running for this player
if not player:FindFirstChild("ClicksTimer") then
local timer = Instance.new("BoolValue")
timer.Name = "ClicksTimer"
timer.Parent = player
-- Give the first win immediately, then every second
giveClicks()
while timer and timer.Parent do
task.wait(1) -- 1 second
giveClicks()
end
end
end
end
end
-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)
I will even send video robloxapp-20240630-1128360.wmv (2.7 MB)
In the video, when I step off the AFK Grinder part it keeps giving me 100 clicks per second where I want it to stop giving me clicks. I hope you can understand this
OP, can you try using this script? Let me know if you find anything wrong
local part = script.Parent -- The part this script is attached to
local amountAdding = 100
local cooldown = 1
-- Function to handle the touch event
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local clicks = player.Data.PlayerData.Currency
if not player:FindFirstChild("afkCheck") then
local afkCheck = Instance.new("BoolValue")
afkCheck.Name = "AFK"
afkCheck.Parent = player
afkCheck.Value = true
elseif player.AFK.Value == false then
local afkCheck = player.AFK
afkCheck.Value = true
end
repeat
task.wait(cooldown)
clicks.Value += amountAdding
until otherPart.TouchEnded
end
end
-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)
local part = script.Parent
local amountAdding = 100
local cooldown = 1
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local clicks = player.Data.PlayerData.Currency
if not player:FindFirstChild("afkCheck") then
local afkCheck = Instance.new("BoolValue")
afkCheck.Name = "AFK"
afkCheck.Parent = player
end
if player.AFK.Value == false then
player.AFK.Value = true
repeat
task.wait(cooldown)
clicks.Value += amountAdding
until otherPart.TouchEnded
end
end
end
part.Touched:Connect(onPartTouched)
Here you go, let me know if you have any problems with this.
local part = script.Parent
local amountAdding = 100
local cooldown = 1
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local clicks = player.Data.PlayerData.Currency
if not player:FindFirstChild("afkCheck") then
local afkCheck = Instance.new("BoolValue")
afkCheck.Name = "AFK"
afkCheck.Parent = player
end
if player.AFK.Value == false then
player.AFK.Value = true
repeat
task.wait(cooldown)
clicks.Value += amountAdding
until otherPart.TouchEnded
player.AFK.Value = false
end
end
end
part.Touched:Connect(onPartTouched)
Looks like I missed another small thing that was creating a bunch of boolValues, this should work now.
local part = script.Parent
local amountAdding = 100
local cooldown = 1
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local clicks = player.Currency
if not player:FindFirstChild("AFK") then
local afkCheck = Instance.new("BoolValue")
afkCheck.Name = "AFK"
afkCheck.Parent = player
end
if player.AFK.Value == false then
player.AFK.Value = true
repeat
task.wait(cooldown)
clicks.Value += amountAdding
until otherPart.TouchEnded
player.AFK.Value = false
end
end
end
part.Touched:Connect(onPartTouched)