change things from a required lua file, corona sdk - lua

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.

Related

Roblox money distribution system not working

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

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" ;)

How to update a variable in corona SDK?

I have a function, which changes the variable from what it was to something new. I am using load-save .json tables to get and load data. How do I update the startmoneyTxt to display the new variable?
My function:
local function unlockBall(event)
ballfirst = loadsave.loadTable("firstBall.json", system.DocumentsDirectory)
currentMoney1 = loadsave.loadTable("cashTable.json", system.DocumentsDirectory)
difference = currentMoney1 - ballfirstUnlock
if(ballfirst == 0 and difference >= 0)then
ballfirstID = 1
loadsave.saveTable(ballfirstID, "firstBall.json", system.DocumentsDirectory)
loadsave.saveTable(difference, "cashTable.json", system.DocumentsDirectory)
end
end
My code which should be updated:
currentMoney = loadsave.loadTable("cashTable.json", system.DocumentsDirectory)
startmoneyTxt= display.newText("$ "..currentMoney.." " , 0,0, "Helvetica", 20)
sceneGroup:insert(startmoneyTxt)
Whenever you want change text use
startmoneyTxt.text = "Your text here"
Note: As names saveTable and loadTable imply functions are indent to save/load tables. So you can use one file to save/load multiple values.
I use loadsave module to save/load setings in my game The Great Pong.

Issues with LUA copy

I am using the following program to copy one file another. I am often seeing source and destination is not exactly the same (md5sum is different). Are there anything wrong with the below code?
local size = 2^13 -- good buffer size (8K)
local params = {...}
local srcfile = params[1]
local outfile = params[1] .. "_copy"
print (srcfile)
print (outfile)
local inf = io.open(srcfile, "r")
local of = io.open(outfile, "w")
while true do
local block = inf:read(size)
print(size)
if not block then break end
of:write(block)
end
inf:close()
of:close()
Thanks,
GL
You may want to use binary mode to ensure endline characters were not modified.
local inf = io.open(srcfile, "rb")
local of = io.open(outfile, "wb")

Saving Player Data in RLua

Is there a reason why these do not work?
Player joining script:
local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue", player)
stats.Name = "leaderstats"
local points = Instance.new("IntValue", stats)
points.Name = "Points"
local credits = Instance.new("IntValue", stats)
credits.Name = "Credits"
local key = "player-"..player.userId
local savedValues = DataStore:GetAsync(key)
if savedValues then
--Save format: (points, credits)
points.Value = savedValues[1]
credits.Value = savedValues[2]
else
local ValuesToSave = {points.Value, credits.Value}
DataStore:SetAsync(key, ValuesToSave)
end
end)
And this other script for when the player leaves.
local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")
game.Players.PlayerRemoving:connect(function(player)
local key = "player-"..player.userId
--Save key: {points, credits}
local valuesToSave = {player.leaderstats.Points.Values, player.leaderstats.Credits.Values}
DataStore:SetAsync(key, valuesToSave)
end)
This is for a game I am working on, justify (RLua is Roblox Lua, if you didn't know).
Yes, more than likely the leaderstat is being removed before you have a chance to extract data.
I would suggest not using leaderstats as reference for data. It's better to store data on the script directly.
However, if you truly must use leaderstats, parent it to somewhere else, extract data, then remove it.
local lead = player.leaderstats
lead.Parent = game
-- extract data
lead:Destroy()
Or you could define all those objects in a variable before they get reparented.
But again, I highly suggest not to use leaderstats for data saving. Exploiters can change that data easily and change the value to a high number.

Resources