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

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

Related

I'm having a problem if the player dies but has clones of the same tool instead of all the clones saving only 1 does how can I fix this?

My end achievement is wanting the duplicate tools to save when the player dies
For example, if the player owns three "Gun 1s" then dies instead of still having three Gun 1s in their inventory they will only have one Gun 1.
This is what I currently got
local Inventory = {}
local function Spawned(Char)
local Plr = game.Players:GetPlayerFromCharacter(Char)
for i,v in pairs(Inventory[Plr]) do
if Plr['Backpack']:findFirstChild(v.Name) then
Plr['Backpack'][v.Name]:Destroy()
end
v.Parent = Plr['Backpack']
end
Inventory[Plr] = {}
Char:WaitForChild('Humanoid').Died:connect(function()
for i,v in pairs({Plr['Backpack'], Char}) do
for ii,vv in pairs(v:GetChildren()) do
if vv:IsA('Tool') then
table.insert(Inventory[Plr], vv:Clone())
vv:Destroy()
end
end
end
end)
end
game.Players.PlayerAdded:connect(function(Plr)
Inventory[Plr] = {}
local Char = Plr.Character or Plr.CharacterAdded:wait()
Spawned(Char)
Plr.CharacterAdded:connect(Spawned)
end)
You are destroying the clones here.
if Plr['Backpack']:findFirstChild(v.Name) then
Plr['Backpack'][v.Name]:Destroy()
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

NodeMcu Lua receive information from app in a server

I developed a simple app in MIT APP Inventor that controls a heat pump water heater.
The app sends a different string for each button pressed and the NodeMcu verifies which button was pressed, as you can see in the code below.
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = ""
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP")
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
local _on,_off = "",""
if(_GET.pin == "ip")then
local ip=wifi.sta.getip()
local ler_ip=string.sub(ip,1,13)
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint("IP="..ler_ip)
elseif(_GET.pin == "05a60")then
sp_temperaturas[5]=60
elseif(_GET.pin == "06a60")then
sp_temperaturas[6]=60
elseif(_GET.pin == "Ferias")then
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint(" Modo ferias ")
modo_ferias()
elseif(_GET.pin == "Parar2")then
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint(" Parar")
end
end)
conn:on("sent", function (c) c:close() end)
end)
And when the button "Ferias" is pressed the system starts the heating process by calling the function modo_ferias().
function aquecer()
local kp=100/10
local ki=5
local kd=5
local tempo_on=0
local tempo_off=0
i=0
local duty=0
erro=sp_temp-t_atual
soma_erro = soma_erro + erro/5;
dif_erro = (erro - erro_ant)/5;
erro_ant = erro;
print(erro.." "..soma_erro.." "..dif_erro)
duty= kp * erro + soma_erro / ki + dif_erro * kd
print(duty)
tempo_on= duty *50
if (tempo_on > 5000) then
tempo_on = 5000
end
tempo_off = 5000 - tempo_on
repeat
i = i + 1
tmr.delay(1000)
until (i >= tempo_off)
gpio.write(8, gpio.HIGH)
repeat
i = i + 1
tmr.delay(1000)
until (i == 5000)
gpio.mode(8, gpio.INPUT)
end
function modo_ferias()
repeat
sair_ferias=0
pressao()
if (pressao_psi <=3)
sair_ferias=1
end
t_atual=ler_temperatura()
local int = string.format("%d", t_atual ) -- parte inteira
local dec = string.format("%.1d", t_atual % 10000) -- parte decimal com uma casa
t_display = int .. "." .. dec
rtc()
dofile("novolcd.lua").cls()
dofile("novolcd.lua").lcdprint("Temp="..t_display.." ST="..sp_temp..data_horas)
sp_temp=40
local horas_ferias=hour
if(horas_ferias==0 or horas_ferias==1 or horas_ferias==2 or horas_ferias==3 or horas_ferias==4 or horas_ferias==5 or horas_ferias==6) then
sp_temp=70
end
if (sp_temp>t_atual) then
aquecer()
end
until (sair_ferias==1)
end
And here is where my problem appears. If I press a button form the app after the "Ferias" button been pressed, the NodeMcu won't know it because the program is in the heating functions and not verifying if the app sended any instruction.
Is there any way to listen the app commands and to do the heating process at the same time?
There are a few things
Global state
because the program is in the heating functions and not verifying if the app sended any instruction
If the commands triggered by pressing the various buttons can not run independently from each other then you need some form of global state to make sure they don't interfere.
Busy loop
repeat
i = i + 1
tmr.delay(1000)
until (i == 5000)
This is a no-go with NodeMCU as it's essentially a stop-the-world busy loop. Besides, tmr.delay is scheduled to be removed because it's abused so often.
Task posting
node.task.post is a possible solution to scheduling tasks for execution "in the near future". You could use this to post task from the on-receive callback rather executing them synchronously.

Retry loop until condition met

I am trying to navigate my mouse on object but I want to create a condition that will check if "surowiec" is still on the screen, if not I want to skip loop and go to another one. After it finish the second one get back to first and repeat.
[error] script [ Documents ] stopped with error in line 12 [error] FindFailed ( can not find surowiec.png in R[0,0 1920x1080]#S(0) )
w_lewo = Location(345,400)
w_prawo = Location(1570,400)
w_gore = Location(345,400)
w_dol = Location(345,400)
surowiec = "surowiec.png"
while surowiec:
if surowiec == surowiec:
exists("surowiec.png")
if exists != None:
click("surowiec.png")
wait(3)
exists("surowiec.png")
elif exists == None:
surowiec = None
click(w_prawo)
wait(8)
surowiec = surowiec
How about a small example:
while True:
if exists(surowiec):
print('A')
click(surowiec)
else:
print('B')
break
A while loop that is True will always run, until it it meets a break to exit the loop. Also have a look at the functions that are available in Sikuli, it can somethimes be hard to find them, that they are available. So here are a few nice ones:
Link: Link 1 and Pushing keys and Regions
The commands that I found myself very usefull are is exists and if not exists, and find that will allow to locate an image on the screen. Then you don't have to find an image over and over again if it stays on the same location. image1 = find(surowiec)

rails 3.2 searching action. how to pass results to another action

I should create a search page in which i have to save in an Array all the results of the searching. I had two problems:
1) I used the following statement:
Company.joins(:references).where(sql_string)
that returns an ActiveRecord:Relation and it's not good for me cause i have to display these results in the index action , in which i use an each statement. So , to overcame this problem i used the to_a.
I checked the .class of my variable and with the to_a it passed from ActiveRecord:Relation to Array. So , it seems that this solve the problem.
Company.joins(:references).where(sql_string).to_a
2) Now, i have to pass this variable (Array) into my index action.
I executed the search in the action called search:
def search
...
#companies = Company.joins(:references).where(sql_string).to_a
end
Now, i want to pass this to index:
def index
#companies ||= Company.all
end
I used #companies ||= Company.all cause i think that the #companies is and istance variable and it should be available in all the actions of the class. Isn't it? By the way, it doesn't workl. I mean , the results are not shared through the two methods.
Also , in the search action i don't know how to call index action. I used the redirect_to but this bring me to another problem.
def search
...
#companies = Company.joins(:references).where(sql_string).to_a
redirect_to companies_index_path
end
The second time i call the search action it brings me into the index action.As i insered the searching value. At really he still had the past searching in memory, and i don't want this behavior.
So , in other words, i want to:
passing #companies searching result to index action.
avoid the loop between search-index. So in every new request resets
the old searching.
i want to know if it's correct the casting with the to_a to bring
an ActiveRecord:Relation to Array.
Thank You.
EDIT:
def search
stringa_sql = ""
ragione_sociale = ""
riferimento = ""
note = ""
min_date = ""
max_date = ""
company_type = ""
sector = ""
country = ""
certification = ""
contact = ""
state = ""
manage = ""
consultation = ""
formation = ""
software = ""
if params[:ragione_sociale]
ragione_sociale = params[:ragione_sociale]
stringa_sql = "ragione_sociale like "+"'%"+ragione_sociale+"%'"
end
if params[:riferimento]
riferimento = params[:riferimento]
stringa_sql += " AND nome like "+"'%"+riferimento+"%'"
end
if params[:note]
note = params[:note]
stringa_sql += " AND note like "+"'%"+note+"%'"
end
if params[:min_date] && params[:min_date]!= ""
if params[:max_date] && params[:max_date]!= ""
min_date = params[:min_date]
max_date = params[:max_date]
stringa_sql += " AND richiamare >="+min_date+" AND richiamare <="+max_date
end
end
if params[:company_type] #se inviamo la richesta senza scrivere params[:category] viene passato vuoto
if params[:company_type][:id] != ""
company_type = params[:company_type][:id]
stringa_sql += " AND id_forma_giuridica = "+company_type
end
end
if params[:sector]
if params[:sector][:id] != ""
sector = params[:sector][:id]
stringa_sql += " AND id_settore = "+sector
end
end
if params[:country]
if params[:country][:id] != ""
country = params[:country][:id]
stringa_sql += " AND id_provincia = "+country
end
end
if params[:certification]
if params[:certification][:id] != ""
certification = params[:certification][:id]
stringa_sql += " AND id_certificazione = "+certification
end
end
if params[:contact]
if params[:contact][:id] != ""
contact = params[:contact][:id]
stringa_sql += " AND id_contattato = "+contact
end
end
if params[:state]
if params[:state][:id] != ""
state = params[:state][:id]
stringa_sql += " AND id_stato = "+state
end
end
if params[:manage]
if params[:manage][:id] != ""
manage = params[:manage][:id]
stringa_sql += " AND id_gestito = "+manage
end
end
if params[:consultation]
if params[:consultation][:id] != ""
consultation = params[:consultation][:id]
stringa_sql += " AND id_consulenza = "+consultation
end
end
if params[:formation]
if params[:formation][:id] != ""
formation = params[:formation][:id]
#formazione DA METTERE
end
end
if params[:software]
if params[:software][:id] != ""
software = params[:software][:id]
stringa_sql += " AND id_software = "+software
end
end
#companies = Company.search(stringa_sql).to_a
if not #companies.empty?
redirect_to companies_index_path
end
end
index:
def index
#companies ||= Company.all
end
I used #companies ||= Company.all cause i think that the #companies
is and istance variable and it should be available in all the actions
of the class. Isn't it?
Not really, it depends on from where you want to access the #companies instance variable. e.g. from which view, you need #companies instance variable in the corresponding action method of the controller.
The second time i call the search action it brings me into the index
action
You are using redirect_to companies_index_path in your search method which brings you to the index action.
To implement search in your application, you can follow this somewhat standard process:
In your application_controller.rb which will have the #search_query.
# Returns string
def search_query
#search_query ||= params[:query] || params[:search]
end
Then, in your searches_controller.rb, you can have:
def search
# in the method build your search results based on
# search_query param
#search_results = Company.joins(:references).where(sql_string(search_query)).to_a
end
In your routes.rb, you can have:
get '/search(/:query)' => 'searches#search', query: /.+/, as: 'search'
Which will take you to the searches_controller's search action where you are building the search results #search_results.
Finally, you need to have a app/views/searches/search.html.erb view file where you have access to your #search_results instance variable and you can just loop through them and display them in this view.
Answers to your last 3 questions:
passing #companies searching result to index action.
avoid the loop between search-index. So in every new request resets
the old searching.
You can overcome these problems by following the request/response flow that I have mentioned above. You should not share your index view with your search and you should not have any loop between search and index. Both of them are separate actions of the controller and can be handled separately as I showed above.
i want to know if it's correct the casting with the to_a to bring an
ActiveRecord:Relation to Array.
You can do that if you want. But, you don't really need it in this use case. You can store the ActiveRecord:Relation in your search_results and when you access this instance variable from inside your search.html.erb view, you can easily loop through using a .each do block. So, you don't have to worry about ActiveRecord:Relation and Array.

Resources