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

How do I get to the .Player in tools?

Asked by 5 years ago

Let's say I want to make a tool when you equip it the name of the tool is the AccountAge of the player. (I don't want to script that its just an example) how would I get to the correct player?

I did this:

tool.Activated:Connect(function()

Tool = script.Parent

PLAYERNAME =  Tool.Parent.Name

PLAYER = game.Players:FindFirstChild(PLAYERNAME)

Tool.Name = PLAYER.AccountAge

end)

but in output a message appeared:

Players.TechGamar.Backpack.Taser.Script:5: attempt to index global 'Tool' (a nil value)

so how do I get to the player?

Please help me.

1 answer

Log in to vote
3
Answered by
starmaq 1290 Moderation Voter
5 years ago

Ok, so first, you may wanna fix your syntax, second, your problem is that when you're saying Tool.Parent.Name, your refering to Backpack, cuz the parent of the tool is backpack, and backpack is parented to your player, now fixing that should be easy. And btw, one of the biggest issues in your script is tahtyoure using global variables, its recommended to alwyas use local vars, and if they are local you always wanna put them outside of your function, or exaclty your scope, (scope is the content inside your function).

im not sure if you did this or not, but in order for this to work, a part named "Handle" should be parented to your tool.

now this should be your script:

local tool = script.Parent
local plrname = tool.Parent.Parent.Name
local plr = game:GetService("Players"):FindFirstChild(plrname)

tool.Equipped:Connect(function()
    tool.Name = plr.AccountAge
end)

(this was tested and it worked) so, if i explain again, we made all vars local and outside of the function, always do that, and we fixed the plr.Name, always expiremnt stuff, see if an obj is parented to another, see if somethin is duable... always fo this stuff https://gyazo.com/cc754edbb2665956c1a7f9c260854ddd

happy to help!

0
oh that gyazo link is mest up xd starmaq 1290 — 5y
0
i was gonna show what is tool parented to when game is running starmaq 1290 — 5y
0
[text](url) please User#5423 17 — 5y
0
howhow? @kingdom5 starmaq 1290 — 5y
View all comments (4 more)
0
he just told u GoldAngelInDisguise 297 — 5y
0
Thank you, but how do I do a target? Say I want make the bricks color to Brown. How would I do that? Thanks anyway if you can't answer this. TechGamar 30 — 5y
0
part.BrickColor = BrickColor.new("Brown") ? GoldAngelInDisguise 297 — 5y
0
?? starmaq 1290 — 5y
Ad

Answer this question