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

I am having trouble getting the player from character?

Asked by 5 years ago

So here is the script for the part that I have. I have tried to make the script register when the player clicks so that it takes away from the value of cash overall in the part, but then also gives the player the equal amount of cash.

01cash = 2000
02 
03script.Parent.ClickDetector.MouseClick:connect(function(hit)
04    cash = cash - 100
05    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
06    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
07    wait(1)
08end)
09if cash == 0 then
10    script.Parent.Transparency = 1
11    script.Parent.CanCollide = false
12    script.Parent.ClickDetector.Destroy()
13    wait(10)
14    cash = 2000
15    script.Parent.Transparency = 0
16    script.CanCollide = true
17    Instance.new("ClickDetector", script.Parent)
18end

11:09:18.610 - Workspace.Part.Script:6: attempt to index local 'player' (a nil value)

That is the error I received when testing it out by clicking on the part.

Here is my leaderboard script that is referenced in the script in my part.

1game.Players.PlayerAdded:connect(function(plr)
2    local leader = Instance.new("Folder", plr)
3    leader.Name = "leaderstats"
4    local cash = Instance.new("IntValue", leader)
5    cash.Name = "Cash"
6end)

2 answers

Log in to vote
4
Answered by
Thernus 75
5 years ago
Edited 5 years ago

Hey! Actually, a ClickDetector immediately returns playerWhoClicked, which is the Player, and not the character. This code is basically trying to get the player from game.Players, which is not a valid character.

This should fix your issue:

01cash = 2000
02 
03script.Parent.ClickDetector.MouseClick:connect(function(hit)
04    cash = cash - 100
05    local player = hit
06    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
07    wait(1)
08end)
09if cash == 0 then
10    script.Parent.Transparency = 1
11    script.Parent.CanCollide = false
12    script.Parent.ClickDetector.Destroy()
13    wait(10)
14    cash = 2000
15    script.Parent.Transparency = 0
16    script.CanCollide = true
17    Instance.new("ClickDetector", script.Parent)
18end
0
Thanks, this worked; it's amazing it was that simple! LordIsaacofBargo 7 — 5y
0
Accept his answer if it helped RiskoZoSlovenska 378 — 5y
0
Sorry, I'm new. LordIsaacofBargo 7 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Not sure what you mean but this should work,if you are using a local script do this:

01local cash = 2000
02local player = game.Players.LocalPlayer --/ Gets the player
03 
04 
05script.Parent.ClickDetector.MouseClick:connect(function(hit)
06    cash = cash - 100
07    --local player = game.Players:GetPlayerFromCharacter(hit.Parent) Nope
08    player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 100
09    wait(1)
10end)
11if cash == 0 then
12    script.Parent.Transparency = 1
13    script.Parent.CanCollide = false
14    script.Parent.ClickDetector.Destroy()
15    wait(10)
16    cash = 2000
17    script.Parent.Transparency = 0
18    script.CanCollide = true
19    Instance.new("ClickDetector", script.Parent)
20end

If you are using a script, well... CHANGE (ik it sucks)

Answer this question