Detect when an variable with letters, numbers etc changes

Hello. Im making an system that needs to detect when an variable changes and run something. I tried myself with

local var = "3fre"
var:GetPropertyChangedSignal("Value"):Connect
(code)
end

But it says that Value is not a valid property name.
Ive looked at other devforums but those were for a bit different things (“as of ive found”)

1 Like

What you’re looking for is a StringValue.

local str = Instance.new('StringValue')

str.Value = '3fre'

str:GetPropertyChangedSignal("Value"):Connect(function()
    -- code
end)
1 Like

Unable to assign property value, string expected. Got instance

error

Can you send the script please ?

Sounds like you’re trying to change the Value to an object. Can you send your code so I can help you with it?

1 Like

short (not full code)

local str = Instance.new("StringValue")
local object = player:GetMouse().Target
str.Value = object
str:GetPropertyChangedSignal("Value"):Connect(function()

long (full code)

local str = Instance.new("StringValue")
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local function f()
	local object = player:GetMouse().Target
	local high = object:FindFirstChildOfClass("Highlight")

	if not high then
		high = Instance.new("Highlight",object)
		high.FillColor = Color3.fromRGB(38, 114, 255)
		high.OutlineColor = Color3.fromRGB(255, 0, 0)
		high.Enabled = not high.Enabled
	end
	high.Enabled = not high.Enabled
	str.Value = object
	str:GetPropertyChangedSignal("Value"):Connect(function()
	oldobject.HighLight.Enabled = false
	end)
end

	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.F then
		local oldobject = object
			oldobject.HighLight.Enabled = false
		
		end
	
			f()
		

	end)

It looks like you’re just wanting to disable the highlight once the target has changed. You can do this by having a global variable for storing the target objects and then checking the new target against it whenever the function is called again.

local player = game.Players.LocalPlayer

local target

local function f()
	newTarget = player:GetMouse().Target
	
	local high = newTarget:FindFirstChildOfClass("Highlight")

	if not high then
		high = Instance.new("Highlight",object)
		high.FillColor = Color3.fromRGB(38, 114, 255)
		high.OutlineColor = Color3.fromRGB(255, 0, 0)
		high.Enabled = not high.Enabled
	end
	
	high.Enabled = not high.Enabled
	
	if newTarget ~= target then
		target.HighLight.Enabled = false
	end
	
	target = newTarget
end
1 Like

Ive added an F key detector and now the errors are "Players.borisglibusic.PlayerScripts.localselect:20: attempt to index nil with ‘HighLight’
borisglibusic is my username and the localscript is named localselect. The localscript is also inside of starterplayerscripts

code now:

local player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local target

local function f()
	newTarget = player:GetMouse().Target

	local high = newTarget:FindFirstChildOfClass("Highlight")

	if not high then
		high = Instance.new("Highlight",object)
		high.FillColor = Color3.fromRGB(38, 114, 255)
		high.OutlineColor = Color3.fromRGB(255, 0, 0)
		high.Enabled = not high.Enabled
	end

	high.Enabled = not high.Enabled

	if newTarget ~= target then
		target.HighLight.Enabled = false
	end

	target = newTarget
end

	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.F then
		f()
	end
	end)

Sorry, just update the if condition to:

if target and newTarget ~= target then
    --code
end

column 20 is at target.HighLight.Enabled = False

no errors but no highlight (thirtie characterss)

No sure what could be causing it not to highlight. I just tested with some similar code to your in studio and it worked fine.

Test:
robloxapp-20240515-1017430.wmv (333.1 KB)

Code:

local Players = game:GetService('Players')
local UserInputService = game:GetService('UserInputService')

local player = Players.LocalPlayer

local target;

local function f()
	local newTarget = player:GetMouse().Target
	
	local high = newTarget:FindFirstChildOfClass("Highlight")

	if not high then
		high = Instance.new("Highlight", newTarget)
		high.FillColor = Color3.fromRGB(38, 114, 255)
		high.OutlineColor = Color3.fromRGB(255, 0, 0)
		high.Enabled = not high.Enabled
	end
	
	high.Enabled = not high.Enabled
	
	if target and newTarget ~= target then
		target.Highlight.Enabled = false
	end
	
	target = newTarget
end

UserInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.F then
		f()
	end
end)
1 Like

It now worked! Thank you!

thirtiess characterrss

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.