ROBLOX INFINITE YIELD Infinite yield possible on 'Players.Aiman123boss.PlayerGui:WaitForChild("Main")' - roblox

i get this infinte yield:
Infinite yield possible on 'Players.Aiman123boss.PlayerGui:WaitForChild("Main")'
i want to fix it so heres my script:
local pg = player:WaitForChild("PlayerGui")
local Main = pg:WaitForChild("Main")
for i, v in pairs(Main.PetShop.ScrollingFrame:GetChildren()) do
if v.Name ~= "Template" and v:IsA("Frame") then
v.PetPrice.Text = game.ServerStorage:WaitForChild("Pets"):FindFirstChild(v.Name).Price.Value
v.PetName.Text = game.ServerStorage:WaitForChild("Pets"):FindFirstChild(v.Name).Name
end
end

You are waiting on the object "Main" here:
local Main = pg:WaitForChild("Main")
Roblox determined that it's possible that "Main" will never be found, because the object doesn't exist completely. Try checking your object hierarchy and determine if the following path exists:
game.StarterGui.Main
If it exists, check if there's any script that might destroy the GUI.

Related

Roblox remoteevents change value

local script
function Click(player)
if game.Workspace.Folder.Value == 0 then local num = 1
elseif game.Workspace.Folder.Value == 1 then local num = 0
game.ReplicatedStorage.Test:FireServer(player ,num)
end
script.Parent.ClickDetector.MouseClick:connect(Click)
end
server script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.Test.OnServerEvent:Connect(function(player ,num)
if player:GetRankInGroup(1) > 248 then
game.Workspace.Folder.Value = num
else
end
end
)
Doesn't work at all, tried testing if it even connects by printing stuff.
You have a bunch of issues.
1. LocalScripts only run in a few locations, and the Workspace isn't one of them.
Based on your code, I'm assuming this LocalScript is a child of a ClickDetector in the Workspace.
See the docs for LocalScripts :
A LocalScript will only run Lua code if it is a descendant of one of the following objects:
A Player’s Backpack, such as a child of a Tool
A Player’s character model
A Player’s PlayerGui
A Player’s PlayerScripts.
The ReplicatedFirst service
So you need to move the LocalScript to a location where it will actually run, then update the path to the ClickDetector.
-- find the detector in the Workspace
local detector = game.Workspace.Part.ClickDetector
detector.MouseClick:Connect(Click)
2. The connection is inside a function that never gets called.
This looks like a typo, but the line where the MouseClick signal is connected to happens inside the Click function, not outside. So effectively, the Click function is declared and never called by anything. You need to move it outside the last end
3. Variables declared using `local` are only accessible at the level (or deeper) that they are declared at.
Let me annotate your code to highlight what's happening...
local function Click(player)
if game.Workspace.Folder.Value == 0 then
local num = 1
-- num stops existing here and Test is never fired
elseif game.Workspace.Folder.Value == 1 then
local num = 0
game.ReplicatedStorage.Test:FireServer(num)
-- Test is fired with a value of 0 only when Folder.Value equals 1
end
end
local detector = game.Workspace.Part.ClickDetector
detector.MouseClick:Connect(Click)
To fix this, you need to move the local num declaration to a higher scope so it is encapsulates the if-statement. Then you need to make sure that the Remote event is fired in both cases, so you should move it outside the if-statement...
function Click(player)
-- define num
local num
if game.Workspace.Folder.Value == 0 then
num = 1
else --if game.Workspace.Folder.Value == 1 then
num = 0
end
-- fire the RemoteEvent
game.ReplicatedStorage.Test:FireServer(num)
end
local detector = game.Workspace.Part.ClickDetector
detector.MouseClick:Connect(Click)
4. (Nit-pick) Bad tabbing in your server Script creates confusion
This is not something you need to fix, but as atomfrog pointed out, nothing is happening in your Script's else block. It is unnecessary to have it at all, and it looks like you tried to use end to escape from the Script, but all you've done is indented your if-statement incorrectly. If you want to escape or for nothing to happen, use return, or don't have an else block.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.Test.OnServerEvent:Connect( function(player, num)
local specialGroupId = 1
local lowestPermissionLevel = 248
if player:GetRankInGroup(specialGroupId) > lowestPermissionLevel then
game.Workspace.Folder.Value = num
end
end)
maybe it doesn't work because you have nothing after the else

Call a script with definitions in a function

We have a script that defines values to names similar to #define in c. For example:
script.m:
ERR_NOERROR = 0;
ERR_FATAL = 1;
This script already exists and is used for value replacement when reading data from files.
Now we have a function (or more) that does some analysis and we would like to use the same definition in this function to avoid magic numbers. But when the script is called from the function we get an error.
Attempt to add "ERR_NOERROR" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
And this does not help much in the understanding of the problem.
The question is how can we make these definitions visible/usable in the functions with having to copying it every time.
Example:
function foo = bar(a)
run(script.m) %also tried running it without the run command
if a == ERR_NOERROR
foo = 5;
else
foo = 6;
end
end
edit:
There was a nested function,below in the function which I was not aware of. This explains the problem.
This kind of scoping error happens when you use nested or anonymous function within a function. The solution is well documented.
To your case, you can avoid nested function, or "Convert the script to a function and pass the variable using arguments", as the documentation suggests.
EDIT: I should have made it clear that the error occurs even if the script is not called within the nested function. Similar scenario is that, in debug mode (by setting up a break point), it will be an error if one tries to create a temporal variable to test something.
This is not a direct answer, rather a recommendation to switch to another method, which will not be mixing scope and workspace.
Instead of defining your constant in a script, you could make a class containing only constant properties. ex: code for error_codes.m:
classdef error_codes
% ---------------------------------------------------------------------
% Constant error code definition
% ---------------------------------------------------------------------
properties (Constant = true)
noerror = 0 ;
fatal = 1 ;
errorlvl2 = 2 ;
errorlvl3 = 3 ;
warning = -1 ;
% etc ...
end
end
I use this style for many different type of constants. For tidiness, I groups them all in a Matlab package directory (The directories which starts with a + character.
The added benefit of using constant class properties is the safety that the values cannot be changed in the middle of the code (your variables defined in a script could easily be overwritten by a careless user).
So assuming my file error_codes.m is placed in a folder:
\...somepath...\+Constants\error_codes.m
and of course the folder +Constants is on the MATLAB path, then to use it as in your example, instead of calling the script, just initialise an instance of the class, then use the constant values when you need them:
function foo = bar(a)
ERR = Constants.error_codes ;
if a == ERR.noerror
foo = 5;
else
foo = 6;
end
or it can works in switch statement too:
switch a
case ERR.noerror
foo = 5 ;
case ERR.warning
foo = 42 ;
case ERR.fatal
foo = [] ;
end

How can i use a while loop in matlab?

I have a problem using a while loop. My main objective is to simulate a Fanno flow problem for a case where the length of the tube is longer than required. This means we have to change the Mach number in the middle. My code is the following.
clc
clear all
close all
P1=1;
T1=273;
Cf=0.005;
Dh=0.15;
G=1.4;
M1=3.0;
Lxstar=0;
M2=1;
Lx=0;
My=0;
Lystar=0;
tol=.001;
L = 6.0;
error=0;
fp = ((1-M1^2)/(G*M1^2))+((G+1)/(2*G))*log(((G+1)*M1^2)/(2*(1+(M1^2*(G-1)/(2)))))
Lstar=(fp*Dh)./(4*Cf)
while Lstar<L
Mx=(M1+M2)./2
fp1=((1-Mx^2)/(G*Mx^2))+((G+1)/(2*G))*log(((G+1)*Mx^2)/(2*(1+(Mx^2*(G-1)/(2)))))
Lxstar= (fp1*Dh)./(4*Cf)
Lx= Lstar-Lxstar
My=((sqrt((2+(G-1)*Mx.^2)/(2*G*Mx.^2-(G-1)))));
fp2=((1-My^2)/(G*My^2))+((G+1)/(2*G))*log(((G+1)*My^2)/(2*(1+(My^2*(G-1)/(2)))))
Lystar=(fp2*Dh)./(4*Cf)
error= Lx+Lystar-L
if error<=tol
break
else
Diff=Lx+Lystar
if Diff<L
Mx=Mx+.01
else
Mx=Mx-.01
end
end
end
When I run it goes smoothly - it does everything I want but once it changes the Mx it runs again with the Mx=M1+M2/2 instead of the corrected Mx = Mx+.01 or Mx= Mx-.01
This line: Mx=(M1+M2)./2 is executed on every loop iteration, even after you assign the new value to Mx. You need to move it out of the loop, like this:
Mx=(M1+M2)./2
while Lstar<L
%other code here...
end
Then when you assign the new value to Mx, it won't be immediately overwritten with the old value.

Preventing Function Overriding in Lua Table

In my program when two functions with the same name are defined for the same table, I want my program to give an error. What's happening is that it's simply just calling the last function and executing it.
Here's a sample code
Class{'Cat'}
function Cat:meow( )
print("Meow!")
end
function Cat:meow()
print("Mmm")
end
kitty = Cat:create()
kitty:meow()
The result of the execution is only: "Mmm"
Instead I want something like an error message to be given.
Unfortunately, __newindex does not intercept assignments to fields which already exist. So the only way to do this is to keep Cat empty and store all its contents in a proxy table.
I don't know the nature of your OOP library, so you'll have to incorporate this example on your own:
local Cat_mt = {}
-- Hide the proxy table in closures.
do
local proxy = {}
function Cat_mt:__index(key)
return proxy[key]
end
function Cat_mt:__newindex(key, value)
if proxy[key] ~= nil then
error("Don't change that!")
end
proxy[key] = value
end
end
Cat = setmetatable({}, Cat_mt)

'Undefined function or variable' in Matlab

Here is my code:
function [im,sindx,end1]=alln(im,i,j,secret,sindx,end1)
slen=length(secret);
p=im(i,j);
neigh= [im(i-1,j) im(i+1,j) im(i,j-1) im(i,j+1) im(i-1,j-1) im(i+1,j-1) im(i-1,j+1) im(i+1,j+1)];
minpix = min (neigh)
maxpix = max (neigh)
if minpix < p < maxpix
lowlim = minpix+1;
highlim = maxpix-1;
range = highlim-lowlim+1;
nbits=floor(log2(abs(range)));
if sindx+nbits-1>slen
end1=1;
return
end
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
b = bin2dec(bin);
newvalue1 = abs (minpix + b);
newvalue2 = abs (maxpix - b);
if abs(p-newvalue1)<= abs(p-newvalue2)
im(i,j) = newvalue1;
else
im(i,j) = newvalue2;
end
sindx=sindx+nbits;
end
end
My main program calls this function. When I run the program, I get the following error message:
??? Undefined function or variable "bin".
Error in ==> alln at 34
b = bin2dec(bin);
I know there are many experts for whom this is not a problem at all. I am new to MATLAB. Please guys, show me the way, which type of modification in the code can overcome this problem?
First of all, are there some lines missing from the file? Perhaps you've stripped some comments from the top? Because the error message says that
b = bin2dec(bin);
is line 34, but it's line 22 in the code you present.
OK, that aside...
The error message says that 'bin' isn't defined, but I see that it's being set on the line...
bin(k)=secret(sindx+k-1);
That suggests to me that THAT line isn't being run.
I see that that bin = ... line is inside of a 'for' loop, so I suspect that the for loop is run zero times, meaning that 'bin' never gets defined. What is nbits? Is it 1, or perhaps less than 1? THAT would prevent the loop from running at all.
Try removing the semicolon from the end of the
nbits=floor(log2(abs(range)));
line and run your code again.
Leaving off the semicolon will force the value of nbits to be printed in the Command Window. I bet you'll find that it's 1 or less. If that's the case, then start looking at HOW nbits is calculated, and I bet you'll find the problem.
At what input arguments to the function alln, are you getting the error?
Lets suppose that nbits is 0, then the following loop will not run:
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
So, bin will be undefined. So, the error happens. This is one of the cases where the error can happen. There are many such possible cases.