Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would you find the player that activated a tool?

Asked by
danglt 185
5 years ago

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)
0
game.Players.LocalPlayer? DinozCreates 1070 — 5y
0
if its a server script then then u can just get the player from the character Gey4Jesus69 2705 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
Ad
Log in to vote
0
Answered by 5 years ago

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.

Answer this question