Simple highscore using preferences (Lua - Corona SDK) - mobile

And thanks in advance for your help!
I'm trying to implement in my game an highscore feature using "preferences" in Lua, but i'm missing something, here's my code:
local highscore_letta = system.getPreference( "app", "highscore", "number" )
-- if preference do not exist, create it and set it to 0 (first time playing the game)
if (highscore_letta == nil) then
local appPreferences =
{
highscore = "0"
}
system.setPreferences( "app", appPreferences )
end
-- if the score is higher than current highscore, update it with the new one
if (_G.player_score > highscore_letta) then
local appPreferences =
{
highscore = _G.player_score
}
system.setPreferences( "app", appPreferences )
end
After the player lose for the first time, the game crashes saying that it's comparing a null value in "highscore_letta".
After a second try, the game do not crashes, but it sticks with 0 and never update it at all.
Any advice? I can't figure out what i'm missing.
Thanks again!

Try
local highscore_letta = system.getPreference( "app", "highscore", "number" ) or 0
-- if the score is higher than current highscore, update it with the new one
if (_G.player_score > highscore_letta) then
local appPreferences =
{
highscore = _G.player_score
}
system.setPreferences( "app", appPreferences )
end

Related

Is there a way to check if any content of a array is in another array in Roblox

So I am trying to make a script which allows me to ban people but the main script which checks if a player is in the game and in the banned users list to be killed or kicked. Here is my code:
local BannedUsers = {"littleBitsman"}
local Players = game.Players:GetChildren()
wait(10)
for index1,value1 in ipairs(Players) do
for index2,value2 in ipairs(BannedUsers) do
if Players[index1] == BannedUsers[tonumber(index2)] then
local HumanoidToKill = workspace[value1].Character:FindFirstChildWhichIsA("Humanoid")
if HumanoidToKill.Health >= 0 then
HumanoidToKill.Health = 0
print("killed " .. tostring(value1))
end
end
end
end
The wait(10) is so I can test the script without executing too early, and the use of my username is for testing.
Also when I do test it it does nothing at all.
You can use the table.find function.
local BannedUsers = {"littleBitsman"}
for _, player in ipairs(game.Players:GetChildren()) do
if table.find(BannedUsers, player.Name) then
player:Kick("You are banned!")
end
end

How do I loop ROBLOX audio at a specific point?

Here's my local script placed into the starter gui. I need the sound to loop after 62 seconds.
game.Workspace.Sound.Play()
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://145294677"
sound.TimePosition = 0
sound:Play()
wait(62)
sound:Stop()
sound:Play()
Sound:Play() will reset the position to 0 or the last set value
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://145294677"
while true do
sound:Play()
wait(62)
end

Roblox Studio Lua if-statement in loop

I have a Roblox Game in this game the time changes using the code on the Roblox Developer site(robloxdev.com) I have been making a door with two unions called "open" and "closed". I want the door to be open between 10 in the morning and 5 in the evening. However the door won't open and it's not even bringing up the print open/close when it is the right time.
This is my current code Note: The script is in the same model (called: Door) as the two unions.
while true do
if game.Lighting.ClockTime > 10 and game.Lighting.ClockTime < 17 then
--Open the door
print("open")
script.Parent.Closed.Transparency = 1
script.Parent.Closed.CanCollide = false
script.Parent.Open.Transparency = 0
script.Parent.Open.CanCollide = true
else
--Close the door
print("close")
script.Parent.Closed.Transparency = 0
script.Parent.Closed.CanCollide = true
script.Parent.Open.Transparency = 1
script.Parent.Open.CanCollide = false
end
end
Thank's for any help.
You should add wait inside the while loop.
while true do
if game.Lighting.ClockTime > 10 and game.Lighting.ClockTime < 17 then
--Open the door
print("open")
script.Parent.Closed.Transparency = 1
script.Parent.Closed.CanCollide = false
script.Parent.Open.Transparency = 0
script.Parent.Open.CanCollide = true
else
--Close the door
print("close")
script.Parent.Closed.Transparency = 0
script.Parent.Closed.CanCollide = true
script.Parent.Open.Transparency = 1
script.Parent.Open.CanCollide = false
end
wait(1) -- change this to whatever you want
end

Prevent Amibroker from entering the same day as its exit in the same symbol

I am attempting to create a share/stock portfolio based system that will enter at the open and possibly exit on the same day at close if the conditions are met. I have this basicaly working. The thing i cant get going is that I would like my stock system to only ever have 1 open postion in a company at any time.
It seems that if there is both an exit and an entry on the same day, amibroker backtesting is allowing the same company to be purchased on the open, if that same company has a sell order on that same day. Here is an example of this:
Notice at point 1 - we would be entering at the open on the 17th
At point 2, we get a sell signal that day, so we should exit at Close on the 24th.
However at point 3 - we have an entry for the same company on the same day.
To be clear - I would like to allow multiple entries on the same day - this is working. The only thing i would like to figure out is to prevent the backtester from entering the SAME company on the SAME day it exits, as due to the system rules, we would have one day of having 2 positions in the 1 company.
Here is the sample code to replicate this:
SetOption("AllowSameBarExit", True );
SetOption("SettlementDelay", 1 );
Buy = C > MA(C,10);
Sell = C < MA(C,10) OR C > O;
// trade on todays open
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = Open;
SellPrice = Close;
SetPositionSize( 20, spsPercentOfEquity );
I have read and re-read the page on portfolio timing: here but I still cant figure out how to prevent the entries for the same company on the same day as an exit.
Any help would be greatly appreciated!
UPDATE
It appears that using the OR C > O in the SELL condition is effecting this. If I remove the OR C > O part, I get the correct behaviour. It is entering on the NEXT day. Now Im wondering how to use that exit without reverting back to same bar same company entry and exit...
Thanks to Tomasz from Amibroker for posting the below solution:
SetOption("AllowSameBarExit", True );
BuyPrice = Open;
SellPrice = Close;
Buy = Ref( Close > MA( Close, 10 ), -1 );
Sell = Close > Open OR Close < MA( Close,10);
// removing buys you don't want
intrade = False;
for( i = 0; i < BarCount; i++ )
{
if( NOT intrade )
{
if( Buy[ i ] )
{
intrade = True;
// same bar sell
if( Sell[ i ] ) intrade = False;
}
}
else
{
if( Sell[ i ] )
{
intrade = False;
Buy[ i ] = False; // remove buy if exited this bar
}
}
}
You can find : a detailed discussion here

Roblox infinite rotating loop

I am working on doing a health pack for Roblox for my game. the code is complete and it works perfectly, but I want the health pack itself to rotate slowly in a cool way so here is my code tell me what is wrong
local healthPack = script.Parent
local healAmount = 30
local cooldown = 5
local canHeal = true
local function handleTouch(otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChild('Humanoid')
if humanoid and canHeal then
if game.Workspace.Player1.Humanoid.Health == 100 then
print("You have enough health")
else
canHeal = false
game.Workspace.HealthPack.Transparency = .75
local currentHealth = humanoid.Health
local newHealth = currentHealth + healAmount
humanoid.Health = newHealth
wait(cooldown)
canHeal = true
game.Workspace.HealthPack.Transparency = 0
end
end
end
healthPack.Touched:connect(handleTouch)
while true do
local currentRotationX = game.Workspace.HealthPack.Rotation.X
--local currentRotationX = game.Workspace.HealthPack.Rotation.Y
local currentRotationZ = game.Workspace.HealthPack.Rotation.Z
game.Workspace.HealthPack.Rotation.X = currentRotationX + 10
--game.Workspace.HealthPack.Rotation.Y = currentRotationY + 10
game.Workspace.HealthPack.Rotation.Z = currentRotationZ + 10
wait(.5)
end
Try the following code. In order to rotate an object correctly (modifying the rotation property usually doesn't do the trick, it's similar to the position property, it conforms to collisions) you must use CFrame.
local x = 0
while true do
game.Workspace.HealthPack.CFrame = game.Workspace.HealthPack.CFrame * CFrame.Angles(0, math.rad(x), 0)
x = x + 1
wait()
end
Fair disclaimer, I haven't worked with RBX.Lua in a while, so this might not be the best way to do it.
local runService = game:GetService("RunService")
runService.Heartbeat:Connect(function()
script.Parent.Orientation += Vector3.new(0,0.2,0)
end)
you could change the y axis (or any other axis) of the part's orientation forever to rotate slowly with runService.Heartbeat (while True do loop but quicker for a smoother rotation).

Resources