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

How do you access Local Player from a normal Script?

Asked by 6 years ago

How do you access a player's "leaderstats" in a normal script, if it is possible?

01game.Players.PlayerAdded:Connect(function(player)
02    local leaderboard = Instance.new("Folder")
03        leaderboard.Name = "leaderstats"
04        leaderboard.Parent = player
05    local score = Instance.new("IntValue")
06        score.Name = "Cash"
07        score.Parent = leaderboard
08        score.Value = 100
09 
10end)

Like for a touch event, I want to increase the player's "Cash" when they touched the part.

1local door = script.Parent
2local player = game.Players.LocalPlayer
3local cash = player:WaitForChild("leaderstats").Cash
4 
5door.Touched:Connect(function()
6    cash.Value = cash.Value +100
7end)
0
Remote events MrGaming4me 28 — 6y
0
Can you elaborate on that more? top500widowxd 23 — 6y
0
stop using local variables outside of their scope DeceptiveCaster 3761 — 6y

2 answers

Log in to vote
4
Answered by
aschepler 135
6 years ago

There is no LocalPlayer in a Script, because the Script runs on the server, which keeps track of all the connected players. So you'll always need some way to figure out which player was involved. In the case of a touch event, this is provided by the argument passed by Touched.

But the object that does the touching isn't actually the Player object where you stored the leaderstats. It's some Part (such as a left lower leg) within the Character, which is a Model associated with the player. So you'll need to get the part's Parent, then use Players:GetPlayerFromCharacter to find the Player.

There's actually a code sample on the documentation page for GetPlayerFromCharacter showing how to use it within a Touched event callback. Modifying that a little to do your change of cash, I get:

01local Players = game:WaitForChild("Players")
02local door = script.Parent
03 
04door.Touched:Connect(function (obj)
05    local player = Players:GetPlayerFromCharacter(obj.Parent)
06    -- player is nil if touched by something that's not a Player
07    if not player then return end
08    local score = player.leaderstats.Cash
09    score.Value = score.Value + 100
10end)
0
best answer right here folks, upvote this DeceptiveCaster 3761 — 6y
0
Amazing, thank you very much top500widowxd 23 — 6y
Ad
Log in to vote
1
Answered by
metryy 306 Moderation Voter
6 years ago

Made a script that indexes the player in the players service by getting the player's name from the object that touched the door's parent. Each time they touch it, their cash value will increase by 100.

1door.Touched:Connect(function(obj)
2    local cash = game:GetService("Players")[obj.Parent.Name].leaderstats.Cash.Value
3    cash = cash + 100
4end)
0
Did not work out, it said something is not a member of Players. Thank you for your time though top500widowxd 23 — 6y
0
That error is probably from something which is not part of a player's Character touching the door, like a wall, terrain, etc. aschepler 135 — 6y

Answer this question