String Value Object 'recentTouched' Returning No Value When Retrieved

Hello :slight_smile: I am currently working on a Simon Says program, and I am having trouble with this checkTouched() function in my main sequence script. Here is some information about how this touch check is supposed to be executed when ran:

Capture

Above is a snip of my workspace tree. Inside I have a Colors folder that contains all the color pads and are labeled by their corresponding color. Each color pad has a touchDetect script that is replicated into each color when the game is executed. What the script does is it detects when it is touched and sets the value of recentTouched to its color as a string value. The following is the touchDetect script:

local part = script.Parent
local colors = script.Parent.Parent
local value = script.Parent.Parent.Parent.recentTouched.Value

local color = script.Parent.Name

part.Touched:Connect(function()
	print("Player Touched " .. color .. "!")
	value = color
	print("Touched value is now: " .. value)
end)

Now with the touch detection explained, this is how the sequence script checks for the correct color value it is expecting when it is checking for a specific color pad to be touched. The following is the main sequence code:

local players = game:GetService("Players")
local colors = game.Workspace.Colors
local touchValue = game.Workspace.recentTouched.Value
local plrName = ""
local array = {}
local arrayStr = ""

-- Get all colors under folder into an array.
for i, item in pairs(colors:GetChildren()) do
	array[i] = item.Name -- Get all color names in array.
	arrayStr = arrayStr .. item.Name .. ", "
end
print("Colors Detected: " .. arrayStr)

-- Pick Randomized Color.
local function pickColor()
	print("Picking color ...")
	local num = math.random(1, 8) --Note to self: Lua arrays start at 1 instead of 0
	local picked = array[num]
	
	print("Picked " .. picked)
	return picked -- Returns string
end

-- Signaling the color plate to the player.
local function signalColor(color)
	local object = colors:FindFirstChild(color)
	object.Material = Enum.Material.Neon
	wait(.5)
	object.Material = Enum.Material.Metal
end

-- Check for when player touches an object.
local function checkTouched(color)
	wait(2) -- 2 seconds to stand on plate
		
	if touchValue == color then
		print("Correct Color Touched!")
		signalColor(color)
                return true
	end
	
	return false
end

-- Main sequence function.
local function initialize(plr)
	print("Player: " .. plr.Name)
	local plrPart = game.Workspace:WaitForChild(plr.Name)
	print("Player " .. plr.Name .. " Model Found.")
	
	local stack = {}
	local gameOver = false
	local iteration = 1
	
	print("Beginning new sequence ...")
	wait(5)
	
	while gameOver == false do
		local clr = pickColor()
		stack[iteration] = clr -- Stack on new Color
		
		-- Signal All Colors
		for index, item in pairs(stack) do
			signalColor(item)
			wait(.5)
		end
		
		-- Detect Touch of All colors
		for index, item in pairs(stack) do
			print("Checking touch for " .. item .. " ...")
			
			local touch = checkTouched(item) -- Returns Boolean
			if not touch then
				print("Player touched incorrect color! Last Touch Value: " .. touchValue .. ".")
				gameOver = true
				break
			end
		end
		
		if gameOver == true then
			break -- Break loop before giving 1+ score.
		end
		
		iteration += 1 -- Adds round finished
	end
	
	plr:Kick("Game Over! You got a HighScore of: " .. iteration)
end

-- When player joins, begin sequence.
players.PlayerAdded:connect(function(plr)
	initialize(plr) -- Pass player added
end)

When it runs its sequence, it will loop through its colors to signal them to the player and then it will loop through the stack again but instead will call the checkTouched() function that will have the color string passed and will return a boolean. This function checks for the string value of recentTouched and compares it to its passed string color value. (Note: recentTouched is a String Value object)

Here is what the problem is. Though the touchDetect script prints out the value of recentTouched when it assigns it to its color as its touched, when checkTouched() checks that same value again, it returns no value though it was assigned before.

Here is the output the game gives when it is ran and a player touches the color pads:

Why is recentTouched returning an empty value when it is retrieved in the sequence script, yet in the touchDetect script it prints out its assigned value and shows that it was properly assigned? Why is this value lost? Sorry this strategy for checking a touched plate may not be the smartest solution :joy:

Thank you in advance for the help :slight_smile:

1 Like

I’ll be reviewing this more in depth momentarily
But I will say you use variables unnecessarily

local function checkTouched(color)
	local success = false
	wait(2) -- 2 seconds to stand on plate

	if touchValue == color then
		success = true
		print("Correct Color Touched!")
		signalColor(color)
	end

	return success -- Returns boolean
end

You don’t need to return success, instead you can simplify that to this

local function checkTouched(color)
	wait(2) -- 2 seconds to stand on plate

	if touchValue == color then
		print("Correct Color Touched!")
		signalColor(color)
        return true
    else
        return false
	end
end
1 Like

Thanks for letting me know, hadn’t noticed that I could have removed that variable. I had another structure for that function before which I edited many times and it must’ve required the variable before. I’ll update the changes :+1:

I am going to assume that the error lies here
B/c when you define a variable as the value, it does not define it as the path

script.Parent.Parent.Parent.recentTouched.Value

So all you are really doing is redefining the variable to: value = script.Parent.Name instead of changing script.Parent.Parent.Parent.ercentTouched


Here’s an example if you are confused, (int value will equal 5 btw)

local number = script.Parent.Parent.IntValue.Value
print(number) -- prints 5
script.Parent.Parent.IntValue.Value = 10
print(number) -- prints 5 still

image


Simple solution is to just redefine the value to this

local value = script.Parent.Parent.Parent.recentTouched

and then just do this

value.Value = color
1 Like

Ah ok I understand this, I will implement this right away and I’ll let you know if it works :+1:

1 Like
local part = script.Parent
local colors = script.Parent.Parent
local rtouched = script.Parent.Parent.Parent.recentTouched

local color = script.Parent.Name

part.Touched:Connect(function()
	print("Player Touched " .. color .. "!")
	rtouched.Value = color
	print("Touched value is now: " .. rtouched.Value)
end)

This is the updated touchDetect script. I implemented it but it is still returning an empty value when it is checked by checkTouched(). The output is the same as in my original post. I am still trying to think of whats causing this but I cannot find the bug.

Make sure to change it in the other script too

1 Like

Ah my bad forgot about the other script :joy: It worked! Thank you for the help, I am still inexperienced at troubleshooting these things alone :joy: Thanks again :+1:

1 Like