Script doesnt properly check/update variable (status) resulting in not functionable script

I wrote this script which is supposed to open a Gui if a variable is a certain thing when clicking T.
If the variable isn’t the certain thing, then it would send a RemoteEvent to the server.

The variable is supposed to change depending on if you click on the button or not.

Well, the issue here is that whenever the script executes and whenever the button gets clicked on, the variable is changing. But whenever the Gui opens, the variable still stays the same as it was in the first instance. Making it always open the Gui and never sending the RemoteEvent even when the variable is supposed to change.

I’ve tried looking over the devforum for similar problems, and rewriting the script multiple times, but it never worked and the results were all the same. I’ve set print(variable) in the scripts and this is the output:
image

I assume that the variable isn’t updating in the script which means it’s still storing the old variable.

By the way, status is the variable we are talking about.

Here is the script:

local button = script.Parent
local Event = game.ReplicatedStorage.Transform
local rs = game:GetService("ReplicatedStorage")
local osound = rs.Music.OmnitrixSound
local GUI = script.Parent.Parent.Parent
local status = "normal" --sets status to normal at begin of script
local ContextActionService = game:GetService("ContextActionService")

local function onActivated()
	if game.Workspace.InCombat.Value == false then
		status = "custom" --changes status to custom (not normal)
		print(status) --prints custom
		Event:FireServer("DiamondHead")
		GUI.Enabled = false
	else
		print("You're Currently in combat!")
	end
end
button.Activated:Connect(onActivated)

local UIS = game:GetService("UserInputService")
--i assume here is the problem
UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then end
	if Input.KeyCode == Enum.KeyCode.T then
		if status == "normal" then --checks if status normal
			if GUI.Enabled == false then
				osound:Play()
				GUI.Enabled = true
			else osound:Play()
			GUI.Enabled = false end end
			print(status) --prints normal for some reason when status was changed to custom earlier in script
		else if status == "custom" then --if not status is normal (doesnt happen, it always stays on normal??)
			Event:FireServer("Reset")
			status = "normal" end --changes back to normal status
	end end)

This is a localscript located in a textbutton btw.

How can i let the variable get across the script better?
Could anyone explain to me what I did wrong or what I can improve? Thanks.

if you try to change the variable’s definition inside the function, it works inside the function, but not on outside of the function

just use return, something like this:

function CreatePart()
   local Part = Instance.new("Part")
   Part.Parent = game.Workspace
   return Part -- if the function is used it will returns the part
end

local Part = CreatePart()
if Part then
   print(Part.Name.. " is created!") -- making sure that it was created
end

It still returns normal
when i put return status which should return custom

I’ve also tried with a BoolValue but whenever the value sets to true it just immediately goes back to false when nothing in the script is setting it to false.

I’m a little confused because your output shows that it changes - could you give another example because your output and the issue you are describing don’t really match up. In your UIS input began event, you are resetting status to “normal” if it is found to be custom, and this agrees with your output.

This also means that the remote event should be firing - if it isn’t then you might have misconfigured it on the server script you are using or there may be a typo somewhere in the name.

The output shows that whenever I CLICK on the text button, the output says that the variable is changed to ‘custom’. But whenever I click T (UIS input) the variable is still ‘normal’, when the textbutton just changed the variable to custom. And that’s the issue. The variable doesn’t get reset to ‘normal’ in the UIS input, because the variable doesn’t show as ‘custom’ in the first place, but stays as ‘normal’. So it couldn’t trigger the resetting of status as it also doesn’t fire the RemoteEvent, but it is properly configured.

Shortly said, whenever i click the textbutton it prints ‘custom’ in the output as, it changed the variable to ‘custom’. But whenever i start the UIS Input the variable is still identified as ‘normal’

Perhaps the issue is this then - your else if statement checking if the status is custom is paired with checking which key you pressed - so if you are clicking the button it is resetting it by firing input began or a mouse move or any input that starts. By the time you press T it has already been reset.

maybe just do only:

return "custom"