Hi @NoobiDerpieDev,
I recently posted a script on your topic which didn’t work. I decided to make the script again so it will work for you, so you can build your game further! I will also explain how I created the script so you understand what the script does.
I will first start with creating the variables for the GreyPart and the BlackPart, so you can check later if it is actually the GreyPart touching the BlackPart, and not something/someone else.
- The normal script is inside of the GreyPart.
- Change the BlackPart to the correct location
local GreyPart = script.Parent
local BlackPart = game.Workspace.Conveyorthing
I see you wanted a cooldown for each time the BlackPart is touched by the GreyPart, so we need variables for that, too.
- The Cooldown is set to false. This means the Cooldown is not active at this time.
- The CooldownTime can be changed to how long you want the Cooldown to last.
local Cooldown = false
local CooldownTime = 10
The player seems to get a random amount of money from 1-20 each time a GreyPart is detected.
local Random = math.random(1,20)
According to you, the part is named after the owner. I want to make a variable for the name of the part, which is the username of the owner of the part.
local Username = GreyPart.Name
We have set the main variables and now we can create the function. What makes the script activate? The touched event.
- Every time something or someone touches the BlackPart, the script gets activated.
Blackpart.Touched:Connect(function(hit)
But you only want to reward the player with money if ‘GreyPart’ touches it. This line will only make the script go on if the something/someone who touched it is GreyPart.
- hit.Name is the name of something or someone who touches it.
- Username refers to the name of the GreyPart.
if hit.Name = Username then
But we still may not reward the player, because we need to check if the Cooldown is over.
- This line stands for ‘If there is no Cooldown (so Cooldown = false), then…’
if not Cooldown then
So now we see that GreyPart touched BlackPart, and the Cooldown is over so we may now reward the player. But how do we know who the player is? We will make a player variable.
- So we go to the list of all players on the server and we use FindFirstChild. Username refers to the name of the GreyPart and the name of the GreyPart is the owner of that part. That is the player.
local Player = game.Players:FindFirstChild(Username)
Most games have a folder named ‘leaderstats’ with all leaderstats inside of it. You want to reward the player with ‘Money’. The Money is inside of the folder, so we need a variable for the folder.
- Please note: the folder in my game is called Leaderstats. Your folder may be called different, please check that. It is also case-sensitive.
local Folder = Player:WaitForChild("Leaderstats")
And the last variable is the Money, the Money is inside of the folder we made a variable of.
- Please note: Money is case-sensitive, change the line if needed.
local Money = Folder:WaitForChild("Money")
Now we will reward the money. Money has a different Value for every player.
- So the Value of the Money of the player + Random (random is the variable we made)
Money.Value = Money.Value + Random
Done, the player received the Money. The last thing we want to do is activate the Cooldown.
- We activate the Cooldown by setting it to true. We made a variable of CooldownTime which means the Cooldown lasts in my case for 10 seconds. Then, the Cooldown is over and is set to false again. The player is now able to get Money again.
Cooldown = true
wait(CooldownTime)
Cooldown = false
The script works in my game, I tested it. I’m not a very good scripter so there may be some things to improve. I hope it helped you, so you understand how to create a script like this. As I said, I’m not very good so this was a great opportunity to expand my knowledge too.
Edit: I don’t know if you want to destroy the part if the player received the money. That is not included in this script, sorry. If you need help with that, let me know.
Here is the script again but then in the entirety.
-- Variables
local GreyPart = script.Parent
local BlackPart = game.Workspace.Conveyorthing
local Cooldown = false
local CooldownTime = 10
local Random = math.random(1,20)
local Username = GreyPart.Name
-- Function
BlackPart.Touched:Connect(function(hit)
if hit.Name == Username then
if not Cooldown then
local Player = game.Players:FindFirstChild(Username)
local Folder = Player:WaitForChild("leaderstats")
local Money = Folder:WaitForChild("Money")
Money.Value = Money.Value + Random
Cooldown = true
wait(CooldownTime)
Cooldown = false
end
end
end)