01 | Part = script.Parent |
02 | player = game.Players.LocalPlayer |
03 | money = player.leaderstats.money -- my problem is in the Output is says "attempt to index nil with 'leaderstats'" why is it saying this? |
04 | Part.Touched:Connect( function (hit) |
05 |
06 | if hit.Name = = "MoneyNow" then |
07 | wait( 0.7 ) |
08 | local function dash() |
09 | player.leaderstats.money.Value = money.Value + hit.Value -- i gave a value to a part called "MoneyNow" and if it touches this part then i wanted it to add its value to the leaderboards-money value which i created. |
10 | end |
11 | hit:Destroy() -- part gets destroyed after the code happends(im trying to get the tycoon money thing where when a part touches the money make part then it gives money to the player then deletes the part. |
12 | end |
13 |
14 |
15 |
16 | end ) |
If you are using a regular script then LocalPlayer does not exist. Instead, you would need to get the player whenever they touch the part and change their leaderstats from there.
Leaderboard script:
01 | game.Players.PlayerAdded:connect( function (p) |
02 | local stats = Instance.new( "IntValue" ) |
03 | stats.Name = "leaderstats" |
04 | stats.Parent = p |
05 |
06 | local money = Instance.new( "IntValue" ) |
07 | money.Name = "Money" -- Change "Money" to anything you want to name it like "Cash" |
08 | money.Value = 50 -- Change the value to how many you want when the player joins the game |
09 | money.Parent = stats |
10 | end ) |
1 | Part.Touched:Connect( function (hit) |
2 |
3 | if hit.Name = = "MoneyNow" then |
unless the part that touched Part
is "MoneyNow" then I don't see how this will connect to the player's leaderstats in any way
1 | local function dash() |
2 | player.leaderstats.money.Value = money.Value + hit.Value |
3 | end |
hit.Value
is not a value because hit
is what touched the part (ex. player character's Right Foot), and I don't understand why is there a local function out of nowhere.
local scripts does not work in workspace, use a regular script instead. here would be a more correct way to do it:
01 | local debounce = false |
02 |
03 | Part = script.Parent |
04 | Part.Touched:Connect( function (hit) |
05 |
06 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
07 | debounce = true |
08 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
09 | local money = player:FindFirstChild( "leaderstats" ).money |
10 |
11 | wait( 0.7 ) |
12 | player.leaderstats.money.Value = money.Value + hit.Value |
13 | Part:Destroy() |
14 | end |
15 |
16 |
17 |
18 | end ) |