So I have this part that gives the player studs (in-game currency). However, there is a cooldown, so I want to try and figure out how to make it so the cooldown doesn’t affect the other players, and each player has their own cooldown. I appreciate all the help I can get!
local part = script.Paren
local cooldown = false
local waitingTime = 15
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not cooldown then
cooldown = true
local player = game.Players:FindFirstChild(hit.Parent.Name)
local leaderstats = player.leaderstats
local Studs = leaderstats.Studs
if Studs then
Studs.Value += 25000
end
wait(waitingTime)
cooldown = false
end
end
end)
local PlayerService = game:GetService("Players")
local Cooldowns = {}
local WaitTime = 15
script.Parent.Touched:Connect(function(Hit)
local Player = PlayerService:GetPlayerFromCharacter(Hit.Parent)
local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
local Leaderstats = Player and Player:FindFirstChild("leaderstats")
local Studs = Leaderstats and Leaderstats:FindFirstChild("Studs")
if Player and Humanoid and Studs and tick() - Cooldowns[Player.UserId] >= WaitTime then
Cooldowns[Player.UserId] = tick()
Studs.Value += 25000
end
end)
PlayerService.PlayerAdded:Connect(function(NewPlayer)
Cooldowns[NewPlayer.UserId] = 0
end)
PlayerService.PlayerRemoving:Connect(function(OldPlayer)
Cooldowns[OldPlayer.UserId] = nil
end)
Method 2
local PlayerService = game:GetService("Players")
local Cooldowns = {}
local WaitTime = 15
script.Parent.Touched:Connect(function(Hit)
local Player = PlayerService:GetPlayerFromCharacter(Hit.Parent)
local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
local UserId = Player and Player.UserId
local Leaderstats = Player and Player:FindFirstChild("leaderstats")
local Studs = Leaderstats and Leaderstats:FindFirstChild("Studs")
if Player and Humanoid and Studs and not table.find(Cooldowns, UserId) then
table.insert(Cooldowns, UserId)
Studs.Value += 25000
task.spawn(function()
task.wait(WaitTime)
table.remove(Cooldowns, table.find(Cooldowns, UserId))
end)
end
end)
I added a second method, both should be working and give the same result.
I used script.Parent for the touched signal, while it is script.Paren in your script, I corrected it thinking it was an error, but if it isn’t then you need to remove the ‘t’.
Edit: Corrected the Player variable, should be working now, sorry for the inconvenience. ^^
I see you are trying to make an object award the player in game currency.
Here are a few things to note.
You could use hashtables in this instance to store each player’s individual cooldown, which is one of the solutions.
When your player has accessories, they will be ignored. According if you want to let the accessories trigger it too, you should use GetAncestor to gain the model (which should be the player’s character), if you don’t want that, then that’s already settled up.
You will be storing each player’s character individually, but when they respawn, there will be uneccesary duplicates, which is not what we want. That’s why you should store the player’s instance instead of their character into the hashtable.
Now, in terms of performance, I asume this is a serverside script that’s in each object. How about this, you put the script in each player and wait each time until their character spawns (don’t forget to disconnect your function), since there will be probably a few of these giving parts, there will be a too much ammount of these listening scripts, which could harm performance. That’s why (also kill bricks) you should put the script into the player. The goal is to create as least as scripts as possible by merging them or putting them in less instances (players) than the oponent (parts that give studs).
Change the global cooldown variable to a table (playerCooldowns) to track each player’s cooldown separately. Replace cooldown = true/false with playerCooldowns[player] = true/nil, and check if not playerCooldowns[player] instead of if not cooldown. Update the player lookup to use game.Players:GetPlayerFromCharacter(hit.Parent) for reliability
local part = script.Parent
local waitingTime = 15
local playerCooldowns = {} -- Table to store cooldowns for each player
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not playerCooldowns[player] then
playerCooldowns[player] = true
local leaderstats = player.leaderstats
local Studs = leaderstats.Studs
if Studs then
Studs.Value += 25000
end
task.wait(waitingTime)
playerCooldowns[player] = nil -- Remove cooldown after waiting
end
end
end)