Roblox money distribution system not working - lua

enter image description hereI was trying to make a money collection system for the person on the stone grey team, but it won't work. What it should do is take the wealth value from the province, multiply by 0.02 and add that onto the total amount of money for the person. The leaderboard has the 'Finances' on there, but the value stays at 0 no matter what. The output bar is also blank.
local map = game.Workspace.map
local rp = game:GetService("ReplicatedStorage")
local provinces = {
map.province1,
map.province2,
map.province3,
map.province4,
map.province5,
map.province6,
map.province7,
map.province8,
map.province9
}
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue")
stats.Name = "leaderstats"
stats.Parent = plr
local finances = Instance.new("NumberValue")
finances.Name = "Finances"
finances.Parent = plr.leaderstats
end)
-- below here i have problems, above this seems to work normally
rp.TaxPaymentsCheque.OnServerEvent:Connect(function(plr)
for start = 1, #provinces do
local provincegrowing = provinces[start]
if provincegrowing.BrickColor == BrickColor.new("Dark stone grey") then
local finances = plr.leaderstats.Finances.Value
local wealth = provincegrowing.ProvinceWealth.Value
local taxmodifier = 0.02
local revenue = wealth * taxmodifier
local oldfinanceamount = finances
finances = oldfinanceamount + revenue
end
end
end)
There is also a picture if it can be of any use.

So the reason why you don't see any change is because you're not actually changing the Finances Value
Instead what you are doing is saving Finances' Value to a variable and changing the variable
local number = SomeIntValue.Value
number = number * 5
-- is practically the same as saying
local number = 1
number = number * 5
It doesn't actually set the IntValue's value
To do that, do something like this
local number = SomeIntValue.Value
number = number * 5
SomeIntValue.Value = number
-- or
SomeIntValue.Value = SomeIntValue.Value * 5

Related

/Lua/ How to do this (idk how to call that lol)

I need to make a trolleybus number, which won't repeat for game. For example, there is a number "101" and there musn't be more "101". How to do that? I have a code, but I know, he won't work and I won't test it lol
function giveNumber()
local number = math.random(100, 199)
local takedNumbers = {}
local i = 0
local massiv = i+1
script.Parent.pered.SurfaceGui.TextLabel.Text = number
script.Parent.zad.SurfaceGui.TextLabel.Text = number
script.Parent.levo.SurfaceGui.TextLabel.Text = number
script.Parent.pravo.SurfaceGui.TextLabel.Text = number
takedNumbers[massiv] = {number}
end
script.Parent.Script:giveNumber() // what I wrote here? idk...
if number == takedNumbers[massiv] then
giveNumber()
end
i didn't test it, because I think it won't work because this code is something bad
I think this will serve your needs.
In the function generateUniqueNumber, the script loops until it found a number that is not yet in the array. (in other words, that it hasn't given out yet)
Once it found that number, it will insert it into the table to remember that it has given it out, and then it will return the number.
Then on the bottom of the script we just give the numbers to the buses :-)
--[[
Goal: Give all buses a unique number
]]
-- Variables
local takenNumbers = {};
-- This function returns a random number in the range [100, 199] that has not been taken yet
function generateUniqueNumber()
local foundNumber = false;
while not foundNumber do
randomNumber = math.random(100, 199);
if not table.find(takenNumbers, randomNumber) then
table.insert(takenNumbers, randomNumber);
return randomNumber;
end
end
end
-- This function sets the number of the bus
script.Parent.pered.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.zad.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.levo.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
script.Parent.pravo.SurfaceGui.TextLabel.Text = tostring(generateUniqueNumber());
2 things:
I didn't test this code as Roblox is not installed on the pc I'm currently on.
Please try formatting your code nicely next time. It greatly improves the readability! For example, you can use this website:
https://codebeautify.org/lua-beautifier
Simpler
Fill a table with free numbers...
local freenumbers = {}
for i = 1, 99 do freenumbers[i] = i + 100 end
...for every new takennumbers use table.remove() on freenumbers
local takennumbers = {}
if #freenumbers > 0 then
takennumbers[#takennumbers + 1] = table.remove(freenumbers, math.random(1, #freenumbers))
end

Roblox Item is lagging

I add a mesh to follow the player where ever he goes. But the mesh is lagging a bit when the player runs. I understand that it is not rendering quickly enough, but anyone know how to add a mesh without it lagging?
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()
backpackItemWorkspace.Parent = game.Workspace.CurrentPets
RunService.Stepped:Connect(function()
local location = humanoidRootPart.CFrame
backpackItemWorkspace.CFrame = location * CFrame.new(2, 2, 3)
end)
Connect the mesh and rootpart using a weld, so you dont need to use RunService to move the mesh everytime.
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()
backpackItemWorkspace.Parent = game.Workspace.CurrentPets
function attach(hroot, mesh)
local weld = Instance.new("WeldConstraint", mesh)
local location = hroot.CFrame
mesh.CFrame = location + Vector3.new(2, 2, 3)
weld.Part0 = hroot
weld.Part1 = mesh
return weld
end
attach(humanoidRootPart, backpackItemWorkspace)
-- please comment if it makes any errors
Ok, for others struggling with having a pet(mesh) follow you smoothly and always stay on your back. I have now spent several hours on this subject and finally got it to work. This is how you do it:
local character = player.Character
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
//where you copy you pet from
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()
//where you keep your pets in the workspace
backpackItemWorkspace.Parent = game.Workspace.CurrentPets
//call the function for attaching the pet
attachPet(backpackItemWorkspace, character, humanoidRootPart)
function attachPet (pet, char, humanoidRootPart)
local focusPart = humanoidRootPart
local newPet = pet
local z = -5
local x = 1
local bodyPos = Instance.new("BodyPosition")
bodyPos.Parent = newPet
bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.Parent = newPet
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
while wait() do
bodyPos.Position = focusPart.Position + focusPart.CFrame.LookVector * z + focusPart.CFrame.RightVector * x
bodyGyro.CFrame = focusPart.CFrame
end
end
Also when you unequip a pet you have to Destroy it from where you store it. In my case I store them in a folder on the player which I call collectionInventory.
local collectionInventory = player:WaitForChild("CollectionInventory")
collectionInventory[petName]:Destroy()
Hope this save anybody else from wasting several hours with research. Even though you will probably learn a lot from those "wasted hours" ;)

Not sure how to use vector3

I tried to make an item spawn every 60 seconds and in one of two locations, but when I tried to use vector3 it didn't spawn in the location of the "spawnplace1" or "spawnplace2", but it spawned ontop of the roka being copied and didn't move. Also I don't think I can hold the copied roka's either. Here's the code!
local roka = workspace["Rokakaka Fruit"]
local itemspawns = workspace.ItemSpawnLocals
local itemspawn1 = itemspawns["Item Spawn 1"]
local itemspawn2 = itemspawns["Item Spawn 2"]
local place1 = itemspawn1.Position
local place2 = itemspawn2.Position
wait(60)
local spawnplace1 = math.random(1,2)
local spawnplace2 = math.random(1,2)
if spawnplace1 == 1 then
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = Vector3.new(itemspawn1)
elseif spawnplace1 == 2 then
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = Vector3.new(itemspawn2)
end
print(spawnplace1)
print(spawnplace2)
The Vector3 holds coordinates of a point in 3D space. You have only provided 1 of 3 pieces of information in the constructor. To construct a Vector3, you need to provide the Y and Z axes as well, like this :
roka2handle.Position = Vector3.new(1, 2, 3)
But you don't need to explicitly create a Vector3 to get your code working. You can just assign the positions of the spawn locations to your newly created fruit, and that should do the trick. This way, you can add lots more spawn locations, and you don't need to update the script each time.
-- grab some things from the workspace
local roka = workspace["Rokakaka Fruit"]
local itemSpawns = workspace.ItemSpawnLocals
-- choose a random spawn location
local spawnLocations = itemSpawns:GetChildren()
local spawnNumber = math.random(1, #spawnLocations)
local spawnPosition = spawnLocations[spawnNumber].Position
-- spawn and move a new fruit to one of the spawn locations
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = spawnPosition
-- debug
print("spawning fruit at : ", spawnPosition)
As a side note, if roka2 is a Model, you may want to consider using roka2:SetPrimaryPartCFrame( CFrame.new(spawnPosition)) to move it around.

change things from a required lua file, corona sdk

When I require a file1, I can export the values, data or objects to file2, but file2 can't export to file1
(With "file" I mean a lua file, example, Scene1.lua, Data.lua)
How can I export from file2 to file1 and viceversa?
example code
Data.lua (file1)
--data.lua
local M = {}
local money = 500
local moneyText = display.newText("Money: " .. money,
display.contentCenterX, display.contentCenterY, "calibri", 50)
M.moneyText = moneyText
M.moneyData = money
return M
file2
--file2.lua
local Data = require("data")
local moneyText2 = Data.moneyText
local moneyData = Data.moneyData
local function addSomeValue()
moneyData = moneyData + 1
end
timer.performWithDelay(1000, addSomeValue, 0)
local Data = require("data")
is more or less equivalent to
function chunkFromDataLua()
local M = {}
local money = 500
local moneyText = display.newText("Money: " .. money,
display.contentCenterX, display.contentCenterY, "calibri", 50)
M.moneyText = moneyText
M.moneyData = money
return M
end
local Data = chunkFromDataLua()
Data now refers to table M
local money = 500
M.moneyData = money
only creates a copy of money and stores that in M. So changing M.moneyData will not affect money. Hence changing Data.moneyData will also not affect money.
In addition to that
local moneyText = display.newText("Money: " .. money,
display.contentCenterX, display.contentCenterY, "calibri", 50)
creates a display object with a static text using the value of money at that moment. So changing the value of money after that won't change the content of your text display. You have to change Data.moneyText.text to do that.

How to design a "Dynamic inventory system" for a point and click game?

I have done lots of research on invetory system for point and click game in Lua and corona.
I have come across this example,I am doing something similar to this,but I need a dynamic inventory system.
I mean if I have 4 slots,and all them are full the fifth object go to next slot,so there will be an arrow to the right so I can click on ;and go to the next page.
And imagine there are 5 items,and I have 4 slots,the fifth slot would be on the next page.
I use the third item,and third slot would then be empty,so I want the fourth and fifth item automatically move back to third and fourth slot.
I have hard time figuring this out.
Thanks for advance.
local myInventoryBag={}
local maxItems = 10 -- change this to how many you want
myInventoryBag[5]=3 -- Hammer for instance
myInventoryBag[4]=7 -- A metal Pipe for instance
local function getImageForItem(thisItem)
local itemNumber = tonumber(thisItem)
local theImage=""
if itemNumber==3 then
theImage="hammer.png"
elseif itemNumber == 7 then
theImage="metalpipe.png"
elseif ... -- for other options
...
else
return nil
end
local image = display.newImage(theImage)
return image
end
local function displayItems()
local i
for i=1,#myInventoryBag do
local x = 0 -- calculate based on the i
local y = 0 -- calculate based on the i
local image = getImageForItem(myInventoryBag[i])
if image==nil then return end
image.setReferencePoint(display.TopLeftReferencePoint)
image.x = x
image.y = y
end
end
local itemImages =
{
[0] = display.newImage('MISSING_ITEM_IMAGE.PNG'),
[3] = display.newImage('hammer.png'),
[7] = display.newImage('metalpipe.png'),
}
function getImageForItem(itemId)
return itemImages[itemId] or itemImages[0]
end
local myInventoryBag={}
local maxItems = 10 -- change this to how many you want
local visibleItems = 4 -- show this many items at a time (with arrows to scroll to others)
-- show inventory items at index [first,last]
local function displayInventoryItems(first,last)
local x = 0 -- first item goes here
local y = 0 -- top of inventory row
for i=first,last do
image = getImageForItem(myInventoryBag[i])
image.x = x
image.y = y
x = x + image.width
end
end
-- show inventory items on a given "page"
local function displayInventoryPage(page)
page = page or 1 -- default to showing the first page
if page > maxItems then
-- error! handle me!
end
local first = (page - 1) * visibleItems + 1
local last = first + visibleItems - 1
displayInventoryItems(first, last)
end
myInventoryBag[5] = 3 -- Hammer for instance
myInventoryBag[4] = 7 -- A metal Pipe for instance
displayInventoryPage(1)
displayInventoryPage(2)
Basically what you would do is loop through all the inventory slots and check if the slot is empty. If it's empty, place the item in that slot and stop the loop. If it's not, go to the next one.
If you want to remove an item from the inventory, you can simply call table.delete(myInventoryBag, slotToEmpty).
For pages, you'd simply have a page variable. When drawing the inventory slots, just loop from slots (page-1) * 4 + 1 to page * 4.
(Edit: I'd highly recommend using proper indentation, as it will make the code much much more readable.)

Resources