LUA loop - help required!

Mila.cs ✅

Well-known member
Joined
Jun 24, 2016
Messages
247
Reaction score
10
Location
̀́̀́
Hey, how can I create an LUA loop similar to the jump "command" in Cleo?

Code:
script_name('HealthRefill')
script_author('Mila, example by FYP')

--- Config
cheatToggle = 'refill'

--- Main
function main()
 while true do
 wait(0)
 if isPlayerPlaying(playerHandle) then
 if testCheat(cheatToggle) then
 activated = not activated
 printStringNow('HealthRefill' .. (activated and '~g~activated' or '~r~deactivated') .. '.~n~~y~Made by Mila~n~~w~Ugbase.eu', 2000)
 if activated then
 setCharHealth(playerPed, 100)
 end
 end
 end
end
end
end

This code works fine, but only one time, then I need to use the command again...
Is there a way to loop it? If yes, how?

I've tried codes like this:
Code:
i = 1
repeat
	 
	 i = i + 1
until i > 5
But I think I used them wrong, as the script wouldn't work anymore.

:sadpepe:
 

veysileth

Active member
Joined
Mar 23, 2017
Messages
85
Reaction score
6
I cant check that. Im on my phone right now. But you want this script to refill your hp forever till deactivqted?  Or just once per cheatcode?
You mean Gosub or register command?

Btw use PLAYER_PED instead of playerped etc. Its better somehow. Fyp magic.


function main()
while true do
wait(0)
if isPlayerPlaying(PLAYER_PED) then

if testCheat(cheatToggle) then
activated = not activated
printStringNow('HealthRefill' .. (activated and '~g~activated' or '~r~deactivated') .. '.~n~~y~Made by Mila~n~~w~Ugbase.eu', 2000)
end --cheatcode close

if activated then
setCharHealth(PLAYER_PED, 100)
end --sethealth close

end --playerplaying close 

end --loop close
end --function main close

Idk if activated = false at start is not needed. Between main and require. 
Btw wtf is iff? 
 

Mila.cs ✅

Well-known member
Joined
Jun 24, 2016
Messages
247
Reaction score
10
Location
̀́̀́
veysileth said:
function main()
while true do
wait(0)
if isPlayerPlaying(PLAYER_PED) then

if testCheat(cheatToggle) then
activated = not activated
printStringNow('HealthRefill' .. (activated and '~g~activated' or '~r~deactivated') .. '.~n~~y~Made by Mila~n~~w~Ugbase.eu', 2000)
end --cheatcode close

if activated then
setCharHealth(PLAYER_PED, 100)
end --sethealth close

end --playerplaying close 

end --loop close
end --function main close

Thanks, that's exactly what I wanted!

veysileth said:
Idk if activated = false at start is not needed. Between main and require.
Btw wtf is iff?

Oh, nevermind that, I just forgot to delete it.
(I copied the main build from a different script as I don't know much about LUA yet)



How could I achieve the same result with a command?
 

veysileth

Active member
Joined
Mar 23, 2017
Messages
85
Reaction score
6
Code:
function main()
sampRegisterChatCommand( "refill", yourcode) -- with function somewhere else
while true do
wait(0)
if isPlayerPlaying(PLAYER_PED) then

if activated then
setCharHealth(PLAYER_PED, 100)
end --sethealth close

end --playerplaying close

end --loop close
end --function main close

function yourcode() -- here's your code for registered cmd
	activated = not activated
	printStringNow('HealthRefill ' .. (activated and '~g~activated' or '~r~deactivated') .. '.~n~~y~Made by Mila~n~~w~Ugbase.eu', 2000)
end

or

Same mechanics as in getCarCoordinates(getCarCharIsUsing(PLAYER_PED)) instead of car = getCarCharIsUsing(PLAYER_PED) + getCarCoordinates(car).
Code:
function main()
sampRegisterChatCommand( "refill",
				  function()
				  activated = not activated
				  printStringNow('HealthRefill ' .. (activated and '~g~activated' or '~r~deactivated') .. '.~n~~y~Made by Mila~n~~w~Ugbase.eu', 2000)
			          end
				  ) -- function within registered cmd.

while true do
wait(0)
if isPlayerPlaying(PLAYER_PED) then

if activated then
setCharHealth(PLAYER_PED, 100)
end --sethealth close

end --playerplaying close

end --loop close
end --function main close

or for bigger scripts and fans of 'clean' stuff.

Code:
function main()
Registerallmycmds()--registering all cmds at the bottom of the script.
while true do
wait(0)
if isPlayerPlaying(PLAYER_PED) then

if activated then
setCharHealth(PLAYER_PED, 100)
end --sethealth close

end --playerplaying close

end --loop close
end --function main close


function Registerallmycmds()
	-- local C = sampRegisterChatCommand --For faster implementation later. Then you can write C( "refill", function() (...)) instead of sampRegisterChatCommand( "refill", function() (...)) every time.

sampRegisterChatCommand( "refill",
				  function()
				  activated = not activated
				  printStringNow('HealthRefill ' .. (activated and '~g~activated' or '~r~deactivated') .. '.~n~~y~Made by Mila~n~~w~Ugbase.eu', 2000)
			          end
				  ) -- function within registered cmd.


end --close of Registerallmycmds
 
Top