Part = script.Parent player = game.Players.LocalPlayer money = player.leaderstats.money -- my problem is in the Output is says "attempt to index nil with 'leaderstats'" why is it saying this? Part.Touched:Connect(function(hit) if hit.Name == "MoneyNow" then wait(0.7) local function dash() 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. end 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. end 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:
game.Players.PlayerAdded:connect(function(p) local stats = Instance.new("IntValue") stats.Name = "leaderstats" stats.Parent = p local money = Instance.new("IntValue") money.Name = "Money" -- Change "Money" to anything you want to name it like "Cash" money.Value = 50 -- Change the value to how many you want when the player joins the game money.Parent = stats end)
Part.Touched:Connect(function(hit) 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
local function dash() player.leaderstats.money.Value = money.Value + hit.Value 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:
local debounce = false Part = script.Parent Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then debounce = true local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) local money = player:FindFirstChild("leaderstats").money wait(0.7) player.leaderstats.money.Value = money.Value + hit.Value Part:Destroy() end end)