3

I am trying to put Rich as a variable, in the middle of print("You are" + print(Rich + 1) + "Rich"). It's inside a function so that whenever the player makes a part, which is going to be money in this case, their richness goes up by one:

    function createPart()
        local Rich = 0 
        local myPart = Instance.new("Part")
        print("You are" + print(Rich + 1) + "Rich")
        myPart.Transparency = 0.5
        myPart. Anchored = true
        myPart.Position = Vector3.new(0,0,0)
        myPart.CanCollide = true
        myPart.Name = "lol" -- how to name a part after instance.new
        myPart.Parent = game.Workspace 
    end
    createPart()

I am stuck with that. Could I get an explanation as to how it would work?

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • if you would read the Lua manual you would know that `print("You are" + print(Rich + 1) + "Rich")` is nonsense in pretty much any thinkable way. Learning Lua's very basics befor trying to use it for Robolox development would make your life so much easier. – Piglet May 09 '20 at 22:52

2 Answers2

2

If you want to increase the variable, you have to store it at a higher scope, otherwise it will be garbage collected at the end of the function call.

local Rich = 0
local function createPart()
    Rich = Rich + 1
    -- print out the message
    -- create the part
end

As for printing it out, you can accomplish this three different ways :

1) comma separation.

The print() function accepts any number of arguments, and will put their values together.

print("You are", Rich,"Rich")

2) string concatenation

In lua, the .. operator is used for string concatenation, not the+ operator.

print("You are" .. tostring(Rich) .. "Rich")

3) string formatting

The lua string library allows you to format your strings by specifying where variables should go in the string. %d can be used to represent an integer.

print(string.format("You are %d Rich", Rich))
Kylaaa
  • 6,349
  • 2
  • 16
  • 27
0

You're very close to your own solution. There are just a couple of mistakes in your attempt. The first is that you need a way to track the player's 'Richness'.

If you want this to appear in the Leaderboard at the top right, you need to attach a 'leaderstats' Value to the player. You can do this by doing the following:

-- somewhere in code, connect to the PlayerAdded event.
game.Players.PlayerAdded:Connect(function(player)
    -- in here, add a folder named 'leaderstats' to the Player. 
    -- IMPORTANT: It must be named 'leaderstats' exactly, all lowercase
    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'

    -- then add the Richness to the added Player. Name if whatever you'd like
    local richness = Instance.new('IntValue')
    richness.Name = 'Richness'
    richness.Value = 0

    -- Parent this Richness value to the leaderstats to make it appear
    -- in the Leaderboard at the top right
    richness.Parent = leaderstats

    -- After you've created this Leaderboard stat, you need to 
    -- parent the leaderstats to the Player
    leaderstats.Parent = player
end)

Now all players will show up on the Leaderboard with a 'Richness' stat. They'll all be 0 until you update this value. You can update the value using RemoteEvents from the Client to the Server.


The Second is that you are printing to the Output window incorrectly. When you merge strings together (called concatenation), in Lua you do so by using '..'. For example:

print('Here is a ' .. 'part of a ' .. 'string')

This will output the following in the 'Output' window:

Here is a part of a string

So to put that into use in your code, you'd type the following:

print('You are ' .. (Rich + 1) .. ' Rich')