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

Not able to get player's backpack in a server script?

Asked by
yoshi8080 445 Moderation Voter
8 years ago

I've been trying to give the player the tool who lowered the value to 0, but it won't work.

local wood = game.ServerStorage.WoodStorage.Wood
local hits = script.Parent.Hits
part = script.Parent
player = script.Parent.Parent.Parent.Parent
timber = false

while wait() do
if hits.Value < 1 and not timber then
timber = true
part:Destroy()
wood:clone().Parent = player.Backpack
end
end

Error: Line 11, attempt to index global 'player' (a nil value)

1 answer

Log in to vote
1
Answered by 8 years ago

Ok, so to get a Player from inside a ServerScript, their are multiple ways to get the player. Now I can see you used player = script.Parent.Parent.Parent.Parent which makes me believe that you have the script inside the StarterPack or StarterGui. If so, then this should be an easy fix. All you have to do is make sure that you are getting the player and not some other random thing. I would suggest using print(player) underneath where you got all the variables;

local wood = game.ServerStorage.WoodStorage.Wood
local hits = script.Parent.Hits
part = script.Parent
player = script.Parent.Parent.Parent.Parent
timber = false

print(player)

You can then check the output and see what was printed. If it prints the players name then it should work, but if it prints anything else, then you haven't got the player, and you might need to add or take away a few .Parents inside the player variable.

If your script ISN'T inside either the StarterPack or StarterGui, then you have to get the player a different way.

But for this, I would suggest that you make sure it is in the StarterPack or StarterGui, and make sure that you got the player, and not something inside the player.

Hope this helped and be sure to +1 and accept answer.

If this didn't, then please reply back with what the print(player) printed in the output and I can help you out.

  • NinjoOnline
0
It does print the player's name, but it returns nil. yoshi8080 445 — 8y
0
Do print(player.Parent) Obviously you must have the player in the workspace or something, cause it should work if it is already printing player. NinjoOnline 1146 — 8y
Ad

Answer this question