Can a variable in a function be used twice with a different name and have a different meaning?

  1. What do you want to achieve?
    So basically I have a variable called “msg” in character.Chatted function, and I want to know if their can be another variable for message but I still want to be able to use the msg variable, so basically two msg variables. The reason why I need this is cause I set a msg to something, and then I want something else to print what the player says but I can’t use msg, because msg already contains something important.

You can just make another variable?

local msg = "something important"
local otherMessage = "something else"

If you really need msg for some reason, then just copy it to the variable.

local msg = "something important"
local msgClone = msg

I mean the problem is that I need the variable in the function let me give an example


game.Players.PlayerAdded(Connect(function(player)

player.Chatted:Connect(function(msg, msg2) -- I want them both to equal the players message



end

end)

Player.Chatted only returns message (arg1) and recipient (arg2) so that wouldn’t work. You can instead do what I said above and copy the message into a new variable.

game.Players.PlayerAdded(Connect(function(player)
	player.Chatted:Connect(function(msg)
		local msgClone = msg
		-- Code
	end
end)

You can find what events return in a function using the Roblox API: Player | Roblox Creator Documentation

but the message needs to be exactly what they said not a determined thing

What do you mean? In my code, if you did print(msg, msgClone) then they would both be the same.

yeah but msg already have a value, I need another variable just like msg that gets the players message but doesnt have the value that msg has

I believe you are wanting to change the name of the variable because you already have a variable with the name msg.

The answer is yes you can. It can be named whatever you like an example would be “shoe”. [EDIT] You would have to declare it as a variable in the script.

What do you mean by “msg that gets the players message but doesn’t have the value that msg has”?

Are you connecting the function to every player? Each player would have their own message value that way.

-- Assuming this is a Server Script

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local newVar = "something words words"
		print(msg)
	end)
end

u see how u have msg in the script u just put, I also have that like that in my script, but I want another one that is the exact same thing, but just rename it to something like msg2, msg already contains something I need, so I need another variable that equals whatever the player types but does not equal directly to msg

Yes you can do that. Instead of typing msg just type msg2 as its a variable name and it can be changed.

Assigning one variable to another does not make it “equal directly”, rather makes it equal to it’s value. The only exceptions are with instances and tables, where assigning a variable to a table variable will become a reference.

I assume that you think that changing “msg” will also change “msg2”. This is not the case.

1 Like

bruh I dont want to rename msg I already know how to I basically want two msg variables both equaling what the player types
without having to put something like

local msg2 = msg

Yeah I have no clue. I don’t know why you’re trying to do this and I feel like you’re asking for something impossible or for the wrong thing. The best I can say right now is no, it is not possible. You can try to give the code and further specify what you want or are trying to do but for now, I’m stuck.

2 Likes

So basically I have a script where, whenever I put !ToggleMic (username) it will pop up with a notification saying the players mic is enabled, and if I put the same command in again a notification will pop up saying mic turned off, but whenever mic is turned on, whatever the player says will be put into a notification and the notification will pop up, but for some reason the chat popping up as the notification part isnt working.

Explorer
image

Server Script

--// VARIABLES \\--

local Players = game:GetService("Players")

local Prefix = '!'

local MicStatus = false

local ReadyToTalk = false


--// FUNCTIONS \\--

local function CheckForTarget (Target)	
	
	if not Target then
		
		print("Target is not valid")
		return false
		
	else
		
		print("Target is valid")
		return true
		
	end
	
end



Players.PlayerAdded:Connect(function(player)
	
	
	
	player.Chatted:Connect(function(msg)
		
		
		
		if msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == false then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)
			
			if Valid then
		
				game.ReplicatedStorage.ToggleMicEvents.MicOnNotifierEvent:FireAllClients(Target)
				
			end
			MicStatus = true
			ReadyToTalk = true
			
		elseif msg:sub(1, 11) == Prefix.."ToggleMic " and MicStatus == true then
			
			local Target = Players:FindFirstChild(msg:sub(12))
			local Valid = CheckForTarget(Target)

			if Valid then

				game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent:FireAllClients(Target)

			end
			MicStatus = false
			ReadyToTalk = false
			
		end
		
	if msg:sub(1, 11) == Prefix.."ToggleMic " and ReadyToTalk == true then

		local Target = Players:FindFirstChild(msg:sub(12))
		local Valid = CheckForTarget(Target)

			if Valid then
				
				game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent:FireAllClients(Target, msg)
				
			end
			
		end
		
	end)

end)

Local Script

game.ReplicatedStorage.ToggleMicEvents.MicOffNotifierEvent.OnClientEvent:Connect(function(Target)
	
	print(Target)
	game.StarterGui:SetCore("SendNotification", {

		Title = "Toggle Mic";
		Text = Target.Name.."'s Mic is off";
		Icon = "http://www.roblox.com/asset/?id=6853258454";
		Duration = "10"

	})

end)

game.ReplicatedStorage.ToggleMicEvents.MicOnNotifierEvent.OnClientEvent:Connect(function(Target)
	print(Target)
	game.StarterGui:SetCore("SendNotification", {

		Title = "Toggle Mic";
		Text = Target.Name.."'s Mic is on";
		Icon = "http://www.roblox.com/asset/?id=257119670";
		Duration = "10"

	})
	
end)

game.ReplicatedStorage.ToggleMicEvents.ChatNotifierEvent.OnClientEvent:Connect(function(Target, msg)
	print(Target)
		game.StarterGui:SetCore("SendNotification", {
		Title = Target.Name;
		Text = msg;
		Duration = "10"

	})
	
end)

Fixed. Now every player can have their mic toggled on or off instead of only one.

MicStuff.rbxl (30.7 KB)