Keybind and if statement struggles for a newbie

  1. What do you want to achieve? well, i’d like to acheive that a certain player (specified with a userid) has a keybind set that executes a script that gives the user a different chat tag.

  2. What is the issue? i’m very new to scripting, so i kind of just frankensteined a couple scripts together and now things are going wrong and i don’t know how to get it to work, whenever i press the keybind it just doesn’t change my chat tag.

  3. What solutions have you tried so far? i’ve tried changing the order of the lines, finding different ways to get the userid of everyone, and i’ve tried numerous tutorials, but none of them is as specific as this. what do i need to do to get this working correctly?

i think it’s also worth mentioning that this script worked if i didn’t include the specific userid part. it works fine until the userid specification.

the “group” half of the chat tag creation part is irrelevant but i got it from a youtube tutorial so oueoghjegu

here is the script i have so far:

local userInputService = game:GetService("UserInputService")



userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
game.Players.PlayerAdded:Connect(function(player)
		if player.UserId == 7937493120 and input.KeyCode == Enum.KeyCode.KeypadPeriod then
		local TextChatService = game:GetService("TextChatService")
		local Players = game:GetService("Players")

		local owners = {7937493120} -- Add user IDs here
		local groupId = nil -- Replace this with the ID of your group

		TextChatService.OnIncomingMessage = function(message)
			local properties = Instance.new("TextChatMessageProperties")
			local textSource = message.TextSource

			if textSource then
				local player = Players:GetPlayerByUserId(message.TextSource.UserId)
				local isOwner = false
				local isInGroup = false

				for i, v in pairs(owners) do
					if v == player.UserId then
						isOwner = true
						break
					end
				end

				if groupId and player:IsInGroup(groupId) then
					isInGroup = true
				end

				if isOwner then
					properties.PrefixText = "<font color='rgb(255,146,37)'>{ARCHER}</font> " .. message.PrefixText
				elseif isInGroup then
					properties.PrefixText = "<font color='rgb(0,255,0)'>[MEMBER]</font> " .. message.PrefixText
				end
			end

			return properties
		end
	end
end 
1 Like

theres a few things you can do to fix your code,

1

Dont nest Playeradded inside a connection, As well as not nesting the oncoming message inside of it
you could instead nest UIS inside the playeradded (as its on the server you’d have to) and unnest the textchatservice to its own scope

2

instead of looping your table to find isowner is true or not, try table.find instead if table.find(owners, player.UserId)

Not sure if this will work or not but give it a try
I removed the groups as you said it’s irrelevant

local Players = game:GetService("Players");
local UIS = game:GetService("UserInputService");
local TextChatService = game:GetService("TextChatService");

local owners = {7937493120} -- Add user IDs here

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	game.Players.PlayerAdded:Connect(function(player)
		if player.UserId == table.find(owners, player.UserId) then
			TextChatService.OnIncomingMessage = function(message)
				local properties = Instance.new("TextChatMessageProperties")
				local textSource = message.TextSource

				if textSource then
					local player = Players:GetPlayerByUserId(message.TextSource.UserId)
					local isOwner = false

					for i, v in pairs(owners) do
						if v == player.UserId then
							isOwner = true
							break
						end
					end

					if isOwner then
						properties.PrefixText = "<font color='rgb(255,146,37)'>{ARCHER}</font> " .. message.PrefixText
					end
				end

				return properties
			end
		end
	end)
end)

it did not work, same problem as last time where it just doesnt change the tag at all

could it be of any use the information that my character already has a chat tag, and my attempt with this script is to change it to something else?

– side question if it’s not too late, i know this is a super rookie stupid dumb question here but where the hell do i put this localscript??? a tutorial told me in startergui but that sounds strange

If that is true then yes, the chat tag script that you posted is probably being overridden by the one that is already there. Are you using another script to create the other chat tag? Or is it part of Roblox?

the other chat tag is another localscript yeah, that one works perfectly because i was able to follow a tutorial haha

though its strange because despite that script being active, without the userid fussbuss, it still worked

heres the other constant chattag’s script

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")

local owners = {7937493120} -- Add user IDs here
local groupId = nil -- Replace this with the ID of your group

TextChatService.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	local textSource = message.TextSource

	if textSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)
		local isOwner = false
		local isInGroup = false

		for i, v in pairs(owners) do
			if v == player.UserId then
				isOwner = true
				break
			end
		end

		if groupId and player:IsInGroup(groupId) then
			isInGroup = true
		end

		if isOwner then
			properties.PrefixText = "<font color='rgb(255,146,37)'>{TEMPO}</font> " .. message.PrefixText
		elseif isInGroup then
			properties.PrefixText = "<font color='rgb(0,255,0)'>[MEMBER]</font> " .. message.PrefixText
		end
	end

	return properties
end

wait but um i might be stupid but where do you specify the keybind here?? the keybind i want is the period on the numberpad and i dont see that anywhere specified gere

oh gosh… thank you so much for the instructions but i’m afraid i can’t follow these instructions too well, not because of you, when i say i’m new, i’m fresh out of the box :sob: i don’t even truly believe i am at the level of coding to even think of doing something as strange and complicated as the one i’m hoping to make right now, but it is required for my game to be satisfying to me :sob: could you um explain a bit further or pprovide examples or anything like that /nf

Is the chat supposed to look like this by the way?

{TEMPO}{ARCHER}YourName: Hi

no no, its supposed to be like, before i hit the keybind it looks like this
{TEMPO}MyName: Hi
and then, after the keybind press,
{ARCHER}MyName: Hi

Try this code out, I simplified the code so it’s easier for me to understand. If this is the intended behavior then I’ll compile and post the complete script.

local UIS = game:GetService("UserInputService")
local TextChatService = game:GetService("TextChatService")

TextChatService.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	local textSource = message.TextSource

	if textSource then
		properties.PrefixText = "<font color='rgb(255,146,37)'>{TEMPO}</font> " .. message.PrefixText
	end

	return properties
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.KeypadPeriod then
		TextChatService.OnIncomingMessage = function(message)
			local properties = Instance.new("TextChatMessageProperties")
			local textSource = message.TextSource

			if textSource then
				properties.PrefixText = "<font color='rgb(255,146,37)'>{ARCHER}</font> " .. message.PrefixText
			end

			return properties
		end
	end
end)

I just combined both of them as there is no reason for them to be in separate files

1 Like

it works perfectly here!!! i just cant test if its specific to only my userid but it works perfectly like that yes

Do you want to make it a switch so you can turn it back to {TEMPO}?

oh, that’d be lovely, sorry for the late reply

Here you go
You can delete the other script, I just put it all in one script
Also I kept the group thing in case you want to use it in the future

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local TextChatService = game:GetService("TextChatService")

local switch = false;
local ownerTag = "<font color='rgb(255,146,37)'>{TEMPO}</font> ";

local owners = {7937493120}
local groupId = nil

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.KeypadPeriod and switch == false then
		ownerTag = "<font color='rgb(255,146,37)'>{ARCHER}</font> ";
		switch = true;
	elseif input.KeyCode == Enum.KeyCode.KeypadPeriod and switch == true then
		ownerTag = "<font color='rgb(255,146,37)'>{TEMPO}</font> ";
		switch = false;
	end
end)

TextChatService.OnIncomingMessage = function(message)
	local properties = Instance.new("TextChatMessageProperties")
	local textSource = message.TextSource

	if textSource then
		local player = Players:GetPlayerByUserId(message.TextSource.UserId)
		local isOwner = false
		local isInGroup = false

		for i, v in pairs(owners) do
			if v == player.UserId then
				isOwner = true
				break
			end
		end

		if groupId and player:IsInGroup(groupId) then
			isInGroup = true
		end

		if isOwner then
			properties.PrefixText = ownerTag .. message.PrefixText
		elseif isInGroup then
			properties.PrefixText = "<font color='rgb(0,255,0)'>[MEMBER]</font> " .. message.PrefixText
		end
	end

	return properties
end

OH MY GOSH!! THIS WORKS!! thank you so much for your help :D!!!

FRIEND!! SOMETHING HAS HAPPENED :frowning: it seems to have perfectly worked in the playtest, but when i applied it to the rea game it DOESNT WORK ANYMORE!! do you have any reason?? is the script in the wrong place??
image

(i’ve 100% deleted the other script!!)

by that i mean, the chattag {TEMPO} will appear, but it will not switch!!

You should probably create another post as this is already a completely separate issue.
Also please don’t remove the Solution to this post as this could help others who come across this same issue as you.

As for the deleted scripts, you posted both of your scripts here so you can just copy paste them back

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