Global variable is changing when it shouldn't

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make the correct global variable static
  2. What is the issue? Include screenshots / videos if possible!
    Global variable is changing when it shouldn’t
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried freezing the table
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
KeybindReinstated.OnClientEvent:Connect(function(currentKeybinds)

	_G.currentKeybinds = currentKeybinds
	_G.legacyKeybinds = currentKeybinds
	table.freeze(_G.legacyKeybinds)

	for k, v in pairs(player.PlayerScripts.Settings.Keybinds:GetChildren()) do
		if _G.currentKeybinds[v.Name] ~= nil then v.Value = _G.currentKeybinds[v.Name] end
	end
	
	local playerKeybindGui = player.PlayerGui:WaitForChild("Settings").SettingsGroup.KeybindToggles
	
	for k,v in pairs(playerKeybindGui:GetDescendants()) do
		if _G.currentKeybinds[v.Name] ~= nil then
			v:WaitForChild("CurrentKB").Text = _G.currentKeybinds[v.Name]
		end
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I didn’t find a post on the forum so I’m making one with an answer, I had to pass through an extra copy of the parameter currentKeybinds and name it something else. For some reason, the above code equated the usage of the two global variables as the same, so whenever I wanted to change one, the other would change. The event is only fired once the player enters the game, so it’s weird why it would act like this. Maybe someone can tell me why.

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