So I am making a game where you have to scoop studs using a stick and push them into a hole, and I want it so each time you push a stud into a hole, you get +1 Studs (in leaderstats which I already have set up)
The problem is, I don’t know how to detect who actually pushes a stud into the hole. So I am wondering how I would go about detecting which exact player pushed the stud into the hole, and giving them the stat. My first thought would be to name the stud after the player who’s Scooper Tool it touched. But I feel as that would be inefficient. So does anybody know a more efficient way to do this, because I honestly do not know where to start.
I myself would go for the name the stud after the player who’s Scooper Tool it touched, but wait for a few more replies to see if someone has a more efficient way of going about it.
Personally, I would create a NumberValue, name it "Owner", set the value to the Player Id, and make the parent the stud. To check if there is an owner just use :FindFirstChild("Owner").
In Roblox, the “Owner” of a part refers to the user who created the place where the part is located. To detect the owner of a part, you will need to do the following:
Find the parent object of the part. You can do this by using the Parent property of the part.
Find the CreatorId of the parent object. This is a property that contains the user ID of the person who created the place.
Use the CreatorId to get the user object of the creator. You can do this by using the GetUserById function of the Players service.
Use the Name property of the user object to get the username of the creator.
Here is some example code that demonstrates how to do this:
local part = game.Workspace.Part
local parent = part.Parent
local creatorId = parent.CreatorId
local creator = game.Players:GetUserById(creatorId)
print(creator.Name .. " is the owner of the part.")
Think of it as a leaderstats, imagine a stat called “Money” and every time you push a part into the hole, i want to give the player who pushed it in, +1 Money
local Players = game.Players
local player = Players:GetUserById(UserId) -- In this case Stud.Owner.Value
local leaderstats = player.leaderstats
local money = leaderstats.Money
money.Value = money.Value + 1