Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My script is not calling a function when finding an not empty string value?

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

1 comment

0

I updated my post

2 answers

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

24 comments

0

It dosen't work for some reason. I checked the value folder to see if it actually does have a value and it does. so I really don't know what the problem is...

0

I added to my post, you can now take a look at the script that puts the value inside the String Value

0

You realise this checks if the value is empty if Values.Location.Value ~= nil then then it print("Call") and the else part happens if the value isn't empty just clarifying

0

ok i will check your script

0

I updated my answer try the new arrangement also if it doesn't work remove GetLocation() and see what happens

0

Does not work. I really thought it was simple, cause it only checks for a emtpy string and if its not empty then it calls my function "RetrieveData()"

0

your not really telling what the function to do

0

My RetrieveData function is cloning a template ui. ill update my post to show you

0

Are all these scripts separate?? or combined into 1

0

ill show you my whole script

0

ok thx I will take a look

0

I took a look at to me it looks fine try using print("Test") to narrow in on the issue also maybe you need to use a remote event to connect the 2 script

0

ok

0

It could just be a simple spelling mistakes also are there any errors in output

0

How would I use a remot event? You dont need to give me a full script, i can script, but if you could give me an idea on how to implement it, that would be great!

0

I will update my answer in a bit

0

ok

0

Will the remote event work even if both my scripts are local scripts? (Both of my scripts are local scripts)

0

If it dosent work can I change the script that receices the event into a server script?

0

I really don't know

0

Yes you can also i think that might be the problem you need to use a scripts also remote events do work with locals

0

alr

0

Let me know if you get it to work

0

I don't really know how to implement that remote event ;-;

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

Resources

  1. https://devforum.roblox.com/t/help-with-checking-if-a-string-is-only-spaces/780592/2 (Original Solution)
  2. https://create.roblox.com/docs/reference/engine/libraries/string#match (string.match() Documentation)
  3. https://developer.roblox.com/en-us/articles/string-patterns-reference (String Patterns)
  4. http://lua-users.org/wiki/StringTrim

This discussion is preserved as it was. New answers, comments, and votes are closed.