Getting my Lua file to update - lua

I'm making an addon for world of warcraft, and I'm to the point where my project is done, but it runs once and is done.
My objective is to make an interface on my screen that shows certain stats, and during combat you may get a proc of some sort and your stats will increase.
Right now my code creates the interface and shows base stats.
This is a function I wrote that I can use to update it correctly.
local function updateFunction()
AgilityLine.text:SetText("Agility = ".. getRangedAgility())
AttackPowerLine.text:SetText("AP = ".. getRangedAttackPower())
CritLine.text:SetText("Crit = ".. getRangedCrit() .."%")
MasteryLine.text:SetText("Mastery = ".. getRangedMastery())
HasteLine.text:SetText("Haste = ".. getHaste() .."%")
end
I test it by making an in game command to run the function. How can I make the function run itself at a reasonable rate, maybe twice a second.

Here's how you update every 2s seconds :
local total = 0
local function onUpdate(self,elapsed)
total = total + elapsed
if total >= 2 then
updateFunction()
total = 0
end
end
local f = CreateFrame("frame")
f:SetScript("OnUpdate", onUpdate)
Just using intern update system for addon =)
Source : http://wowprogramming.com/snippets/Create_a_mini-timer_using_OnUpdate_3

you should add this function into the game loop function, maybe like timetick(); or you should write the timetick function yourself and call this updateFunction

Related

attempt to yield across metamethod/C-call boundary AND cannot resume non-suspended coroutine in ROBLOX STUDIO when using wait (00.1)

This is my code
-- Script by supermarioclub231 as known as marioroblox102, and special thanks to jacko for some scripts things
-- idk
-- Player
local plr = game.Players:CreateLocalPlayer(0)
game:GetService("Visit")
game:GetService("RunService"):run()
plr:LoadCharacter()
-- have to do this so that the same numbers arent generated every time
math.random(); math.random(); math.random()
digits = 4 -- the amount of times to add digits to the end of the player name
prefix = "NoName "
suffix = "" -- wouldnt wanna type anything here, the digits will be added here!
for i=1, digits do
suffix = suffix .. math.random(1,9)
i = i + 1;
end
plr.Name = prefix .. suffix
-- shopium brings to you...
shirt = Instance.new("Shirt", plr)
pants = Instance.new("Pants", plr)
shirt.ShirtTemplate = "rbxasset://shirts/jared.png"
pants.PantsTemplate = "rbxasset://pants/jeans.png"
while true do
wait (0.001)
if plr.Character.Humanoid.Health <= 0 then
wait(5)
plr:LoadCharacter(true)
elseif plr.Character.Parent == nil then
wait(5)
plr:LoadCharacter(true)
end
end
everytime i run it, i get this
the line 39 is wait(000.1),
why its not working?
its supossed to do something forever
like a loop
but it gives me a error
First of all you don't have to you use math.random() three times at the start so it doesn't generate the same script everytime as thats not how it works it will generate a random number between the range you specify every time and the number may be the same every once in a while but thats why its called math.random() but I'm also working on fixing the rest of you script just wanted to tell you that. Also just use the ROBLOX Health Changed Event and the link to the wiki of that event is here.

Can I make my Lua program to sleep for AROUND a day?

I want to make my mydicebot (by seuntje) Lua program sleep AROUND A DAY, after betting for a day... like
function sleep(n)
t = os.clock()
while os.clock() - t <= n do
-- nothing
end
end
function playsleep()
sec = math.random(80000,90000)
sleep(sec) -- around 86400 seconds
end
timestart = os.time()
dur = math.random(70000,80000)
function dobet()
if os.time() - timestart < math.random then
playsleep()
end
timestart = os.time() -- reset the time counter
end
but when I call the playsleep function in the dobet function
it ends up I cannot click anything in my program, cannot move another tab also
and the CPU is not sleeping either, even get busy
and sometimes it stucks even after 90000 seconds
-- THE QUESTIONS --
A. so can I make a function where the sleep is a real sleep?
B. can it sleep until 90000 seconds?
C. or what is the max number of sleep in seconds for the variable "sec" above?
Use the posix module to sleep:
posix = require("posix")
posix.sleep(86400)
But this will still block your program and you won't be able to click anything. You will need to provide more detail about your program in order to receive better advice.
Also os is there.
Why not...
do os.execute('$(type -path sleep) '..(3600*24)) end
...?

Getting the value of an "IntValue" always gets default value

I am a student and I am trying to get the value of an "IntValue" to use has level, what I mean by this is that I need to have a skill level for each individual player and use this skill level to multiply the amount of damage the skill does.
for example: Skill level is 5
the damage should be: baseDamage * SkillLevel
in my case. base damage is 2 so the end result should be 10 damage.
but when I try doing this whit code it doesn't work. (I'm not the best at LUA and I'm fairly new to stack so I apologize in advance)
Code (So far I got this):
local XP = 0 --Exp Amount
local LevelValue = player.Backpack.ScriptStorage.Player.SkillLevel.Value --Gets the value of the skill level from the "IntValue"
--Other code that I don't want to show (it just checks if a remote event has fired the server, and it adds .5 to the XP every time it fires)
--This is the line that should add 1 to the level
LevelValue = LevelValue + 1
--But everytime it gets to 2 it simply gets set back to 1 (the default level)
I just showed the relevant pieces of code. everything that was not relevant to this wasn't shown (except for: XP = XP + .5 which is in the code I'm not showing)
hope this helps figure out what the problem is. as said above: "I'm not the best at LUA and I'm fairly new to stack so I apologize in advance"
In your code, you store SkillLevel.Value into the LevelValue local variable. This takes a snapshot of that value and stores it in the variable. So when you modify the local variable, you are not updating the IntValue object that is storing SkillLevel.
When you want to update SkillLevel, you need to update the IntValue directly :
local SkillLevel = player.Backpack.ScriptStorage.Player.SkillLevel
local LevelValue = SkillLevel.Value
-- .. do some other stuff
-- add 1 to the level
SkillLevel.Value = SkillLevel.Value + 1

I want lua function to run once

I'm quite new to lua scripting.. now i'm trying to code in game boss
local function SlitherEvents(event, creature, attacker, damage)
if(creature:GetHealthPct() <= 60) then
creature:SendUnitYell("Will punish you all",0)
creature:RegisterEvent(AirBurst, 1000, 0) -- 1 seconds
return
end
end
this should make the boss talk when his health = 60% or less but it should run one time, when I run the code the boss keep saying and attacking all the time. How can I make it run once?
Use a boolean created outside the scope of the function callback:
local has_talked = false
local function SlitherEvents(event, creature, attacker, damage)
if creature:GetHealthPct() <= 60 and not has_talked then
has_talked = true
creature:SendUnitYell("Will punish you all",0)
creature:RegisterEvent(AirBurst, 1000, 1) -- 1 seconds
return
end
end
EDIT
If you are actually using the Eluna Engine's RegisterEvent call, then set the number of repeats to 1 and not 0. This will resolve the issue you had.

Easy Lua profiling

I just started with Lua as part of a school assignment. I'd like to know if there's an easy way to implement profiling for Lua? I need something that displays allocated memory, variables in use regardless of their type, etc.
I've been finding C++ solutions which I have been able to compile successfully but I don't know how to import them to the Lua environment.
I also found Shinny but I couldn't find any documentation about how to make it work.
There are several profilers available that you can check, but most of them target execution time (and are based on debug hook).
To track variables, you will need to use debug.getlocal and debug.getupvalue (from your code or from debug hook).
To track memory usage you can use collectgarbage(count) (probably after collectgarbage(collect)), but this only tells you total memory in use. To track individual data structures, you may need traverse global and local variables and calculate the amount of space they take. You can check this discussion for some pointers and implementation details.
Something like this would be the simplest profiler that tracks function calls/returns (note that you should not trust absolute numbers it generates, only relative ones):
local calls, total, this = {}, {}, {}
debug.sethook(function(event)
local i = debug.getinfo(2, "Sln")
if i.what ~= 'Lua' then return end
local func = i.name or (i.source..':'..i.linedefined)
if event == 'call' then
this[func] = os.clock()
else
local time = os.clock() - this[func]
total[func] = (total[func] or 0) + time
calls[func] = (calls[func] or 0) + 1
end
end, "cr")
-- the code to debug starts here
local function DoSomethingMore(x)
x = x / 2
end
local function DoSomething(x)
x = x + 1
if x % 2 then DoSomethingMore(x) end
end
for outer=1,100 do
for inner=1,1000 do
DoSomething(inner)
end
end
-- the code to debug ends here; reset the hook
debug.sethook()
-- print the results
for f,time in pairs(total) do
print(("Function %s took %.3f seconds after %d calls"):format(f, time, calls[f]))
end

Resources