Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would you find the player that activated a tool?

How can i make this script find the player who activated the tool?

local clickable = true
script.Parent.Activated:Connect(function(plr)
    if clickable then
        clickable = false
        plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 5
		plr.leaderstats.Total.Value = plr.leaderstats.Total.Value + 1
        wait(5)
        clickable = true
    end
end)

2 comments

0

game.Players.LocalPlayer?

0

if its a server script then then u can just get the player from the character

2 answers

✓ Accepted answer

Try using game.Players.LocalPlayer? I have added a print to find the player. Basically it just finds the player name and prints it by printing plr.Name

I DON'T KNOW WHAT YOU MEAN BY: "How can i make this script find the player who activated the tool?" so I have made it print the username. And made a script you can work off.

eg.

local plr = game.Players.LocalPlayer
local clickable = true
script.Parent.Activated:Connect(function()
    if clickable then
        clickable = false
        plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 5
        plr.leaderstats.Total.Value = plr.leaderstats.Total.Value + 1
        print (plr.Name) --find him lol
        wait(5)
        clickable = true
    end
end)

or work off this

local plr = game.Players.LocalPlayer
local clickable = true
script.Parent.Activated:Connect(function()
    if clickable then
        clickable = false
        plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 5
        plr.leaderstats.Total.Value = plr.leaderstats.Total.Value + 1
        local playerwhodidit = plr.Name --work off this
        wait(5)
        clickable = true
    end
end)

tool.Activated does not give you the player that activated the tool. But as already suggested by gioni01 you can get the player from the character, with the :GetPlayerFromCharacter() method of the Players service.

local clickable = true
local tool = script.Parent -- variable for the tool
local Players = game:GetService("Players")

tool.Activated:Connect(function()
    local plr = Players:GetPlayerFromCharacter(tool.Parent)
    if clickable then
        clickable = false
        plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 5
        plr.leaderstats.Total.Value = plr.leaderstats.Total.Value + 1
        wait(5)
        clickable = true
    end
end)

tool.Parent is the character model since when a tool is equipped it is parented to the character.

This discussion is preserved as it was. New answers, comments, and votes are closed.