Hello!
I have a problem. I have a script that checks if a string value is NOT emtpy, and if it is not empty, then it will call a function. But for some strange reason, when the string value has some text inside it, it wont call the function!
Here is my code:
while wait(0.1) do if Values.Location.Value ~= nil then print("Call Received") --RetrieveData() end end
So, in the code above, it loops the code forever. If it finds the value called "Location" and that value HAS some text in it it should print("Call Received") but it does not! Anyone know what the problem is?
Here is the code that puts the value inside the string value:
local Locations = {"Downtown", "Suburbs", "Urban", "Rural"} function GetLocation() if LocationBox.Text == Locations[1] then print("Downtown Chosen") Location.Value = "Downtown" elseif LocationBox.Text == Locations[2] then print("Suburbs Chosen") Location.Value = "Suburbs" elseif LocationBox.Text == Locations[3] then print("Urban Chosen") Location.Value = "Urban" elseif LocationBox.Text == Locations[4] then print("Rural Chosen") Location.Value = "Rural" end end CallButton.MouseButton1Click:Connect(function() if allowedToCall == true then GetLocation() else print("You need to choose an Emergency Service!") end end)
Here is my RetrieveData function:
function RetrieveData() local newTemplate = Template:Clone() newTemplate.CallFromLabel.Text = "Active Call" newTemplate.Team.BackgroundColor3 = Player.TeamColor.Color newTemplate.Parent = Container.CallsContainerFrame end
Here is my whole script which includes the RetreiveData function and the loop that calls it:
local Teams = game:GetService("Teams") local RS = game:GetService("ReplicatedStorage") local Player = game:GetService("Players").LocalPlayer local TweenService = game:GetService("TweenService") local Gui = script.Parent.Parent.PoliceRadioGui local Values = RS:WaitForChild("Values") local Location = Values:WaitForChild("Location") local Template = script.Template local Container = script.Parent.Container local CallContainer = script.Parent.CallContainer local InfoContainer = CallContainer:WaitForChild("InformationContainer") local CloseButton = CallContainer:WaitForChild("CloseButton") while wait(0.1) do if Player.Team.Name == "Police" then Gui.Container.Visible = true else Gui.Container.Visible = false end end function RetrieveData() local newTemplate = Template:Clone() newTemplate.CallFromLabel.Text = "Active Call" newTemplate.Team.BackgroundColor3 = Player.TeamColor.Color newTemplate.Parent = Container.CallsContainerFrame end while wait(0.1) do if Location.Value == "Downtown" then print("Call Received") --RetrieveData() elseif Values.Service.Value == nil then print("Text in value") end end
And here is my other sciprt which sets the values inside the Values folder:
local Teams = game:GetService("Teams") local RS = game:GetService("ReplicatedStorage") local Values = RS:WaitForChild("Values") local Location = Values:WaitForChild("Location") local Container = script.Parent.Container local EmergencyContainer = Container:WaitForChild("EmergencyContainer") local LocationContainer = Container:WaitForChild("LocationContainer") local LocationLabelContainer = LocationContainer:WaitForChild("Container") local PCB = EmergencyContainer:WaitForChild("Poison Control") local FFB = EmergencyContainer:WaitForChild("Firefighter") local PB = EmergencyContainer:WaitForChild("Police") local hasCalledPCB = false local hasCalledFFB = false local hasCalledPB = false local LocationBox = Container:WaitForChild("LocationBox") local SituationBox = Container:WaitForChild("SituationBox") local CallingLabel = Container:WaitForChild("CallingLabel") local CallButton = Container:WaitForChild("CallButton") local allowedToCall = false local GottenCall = Values.GottenCall local Locations = {"Downtown", "Suburbs", "Urban", "Rural"} function GetLocation() if LocationBox.Text == Locations[1] then print("Downtown Chosen") Location.Value = "Downtown" elseif LocationBox.Text == Locations[2] then print("Suburbs Chosen") Location.Value = "Suburbs" elseif LocationBox.Text == Locations[3] then print("Urban Chosen") Location.Value = "Urban" elseif LocationBox.Text == Locations[4] then print("Rural Chosen") Location.Value = "Rural" end end PCB.MouseButton1Click:Connect(function() CallingLabel.Text = "Currently Calling : "..PCB.Name hasCalledCB = true allowedToCall = true end) FFB.MouseButton1Click:Connect(function() CallingLabel.Text = "Currently Calling : "..FFB.Name hasCalledFFB = true allowedToCall = true end) PB.MouseButton1Click:Connect(function() CallingLabel.Text = "Currently Calling : "..PB.Name hasCalledPB = true allowedToCall = true end) function CallCB() if hasCalledCB == true then allowedToCall = true else allowedToCall = false end end function CallFFB() if hasCalledFFB == true then allowedToCall = true else allowedToCall = false end end function CallPB() if hasCalledPB == true then allowedToCall = true else allowedToCall = false end end function FocusLost(enterPressed) if enterPressed then local text = SituationBox.Text Values.Situation.Value = text print(text) end end SituationBox.FocusLost:Connect(FocusLost) CallButton.MouseButton1Click:Connect(function() if allowedToCall == true then if hasCalledPCB then print("Emergency Service called: Poison Control") Values.Service.Value = "Poison Control" elseif hasCalledFFB then print("Emergency Service called: Firefighter") Values.Service.Value = "Firegfighter" elseif hasCalledPB then print("Emergency Service called: Police") Values.Service.Value = "Police" end else print("You need to choose an Emergency Service!") end end)
Thank you for your time, KoalaKo_XD
This should work now. You forgot to use else
in your code
while wait(0.1) do if Values.Location.Value ~= nil then print("Call Received") --RetrieveData() else print("text in value") end end
Hope this helps!! Updated I rearranged the script maybe that was the issue try this: if it does not work I got another way to rearrange it
local Locations = {"Downtown", "Suburbs", "Urban", "Rural"} CallButton.MouseButton1Click:Connect(function() if allowedToCall == true then GetLocation() if LocationBox.Text == Locations[1] then print("Downtown Chosen") Location.Value = "Downtown" elseif LocationBox.Text == Locations[2] then print("Suburbs Chosen") Location.Value = "Suburbs" elseif LocationBox.Text == Locations[3] then print("Urban Chosen") Location.Value = "Urban" elseif LocationBox.Text == Locations[4] then print("Rural Chosen") Location.Value = "Rural" else print("You need to choose an Emergency Service!") end end end end)
Remote event to send
game.ReplicatedStorage.EventName:FireServer()
To function one
game.ReplicatedStorage.EventName.OnServerEvent:Connect(function()
Make sure you put remote event in replicated storage and name it
The Value of a StringValue ALWAYS exists. If Value is empty, it is equal to ""
which is an empty string, and an empty string is NOT EQUAL to nil (null/nonexistent). To REALLY check if string is empty or full of spaces, you should use string.match()
and use the pattern ^%s*$
. If the string is empty or full of spaces, string.match()
should return the string, otherwise nil
.
Also take note that any part of the code after a loop won't run until it is stopped either by break
or achieving the goal.
Here is my whole script which includes the RetreiveData function and the loop that calls it:
local Teams = game:GetService("Teams") local RS = game:GetService("ReplicatedStorage") local Player = game:GetService("Players").LocalPlayer local TweenService = game:GetService("TweenService") local Gui = script.Parent.Parent.PoliceRadioGui local Values = RS:WaitForChild("Values") local Location = Values:WaitForChild("Location") local Template = script.Template local Container = script.Parent.Container local CallContainer = script.Parent.CallContainer local InfoContainer = CallContainer:WaitForChild("InformationContainer") local CloseButton = CallContainer:WaitForChild("CloseButton") function RetrieveData() local newTemplate = Template:Clone() newTemplate.CallFromLabel.Text = "Active Call" newTemplate.Team.BackgroundColor3 = Player.TeamColor.Color newTemplate.Parent = Container.CallsContainerFrame end while task.wait(0.1) do if Player.Team.Name == "Police" then Gui.Container.Visible = true else Gui.Container.Visible = false end if Location.Value == "Downtown" then print("Call Received") RetrieveData() elseif Location.Value:match("^%s*$") then -- similar to string.match(Location.Value, "^%s*$"); checks if Location.Value is either empty or full of spaces print("Call Received") RetrieveData() elseif Values.Service.Value == nil then print("Text in value") end end
string.match()
Documentation)