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.
01 | cash = 2000 |
02 |
03 | script.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 ) |
08 | end ) |
09 | if 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) |
18 | end |
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.
1 | game.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" |
6 | end ) |
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:
01 | cash = 2000 |
02 |
03 | script.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 ) |
08 | end ) |
09 | if 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) |
18 | end |
Not sure what you mean but this should work,if you are using a local script do this:
01 | local cash = 2000 |
02 | local player = game.Players.LocalPlayer --/ Gets the player |
03 |
04 |
05 | script.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 ) |
10 | end ) |
11 | if 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) |
20 | end |
If you are using a script, well... CHANGE (ik it sucks)