Hello guys! In my script I wanted to add to it where if the player stops touching the part it will stop giving clicks. I do not have leaderstats and don’t want to add it but I do have currency which is located inside of {player}.Data.PlayerData
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 'Clicks' 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)
my fault a debouce would not be the solution for your issue however see if this script works. if so please make my comment the solution.
local function onPartTouched(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
if not player:FindFirstChild("leaderstats") then
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats
end
local wins = player.leaderstats:FindFirstChild("Wins")
if wins then
local function giveWin()
wins.Value = wins.Value + 1
end
if not player:FindFirstChild("WinTimer") then
local timer = Instance.new("BoolValue")
timer.Name = "WinTimer"
timer.Parent = player
giveWin()
while timer and timer.Parent do
task.wait(300) -- 5 minutes
giveWin()
end
end
end
end
end
local function onPartTouchEnded(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player and player:FindFirstChild("WinTimer") then
player.WinTimer:Destroy()
end
end
part.Touched:Connect(onPartTouched)
part.TouchEnded:Connect(onPartTouchEnded)
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)