How do you make a function fire whenever a player chats?

Is there a preset function that connects to whenever a player chats? I’m trying to make it so that a player get’s a certain amount of XP whenever they chat with a cool down of 30 seconds (to prevent spamming). I’m fairly new to scripting so I haven’t a clue to where to even program something like this.

Hello I found an API reference that I think might help you.

Just tell me if you need any further help creating your script.

2 Likes
-- local script example

local player = game:GetService("Players").LocalPlayer -- the player

local canChat = true
local coolDown = 30 -- default cool down

player.Chatted:Connect(function(message) -- detect when the player chats
   if canChat then -- checks if the "canChat" Boolean is true
       canChat = false -- disable it for the cool down

        coroutine.wrap(function() -- used to prevent thread yielding 
            wait(coolDown)
            canChat = true -- re enable after the cooldown
        end)()

         -- do stuff
   end
end)
4 Likes

Thanks for the reply, but a few questions if that’s alright. This is my current level system:


game.Players.PlayerAdded:Connect(function(player)
	local level = Instance.new("IntValue", player)
	level.Name = "Level"
	level.Value = 1
	
	local exp = Instance.new("IntValue", level)
	exp.Name = "Current"
	exp.Value = 0
	
	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 100
	
	-- continous loop
	
		function additionXP()
				player.Level.Current.Value = player.Level.Current.Value + 25
		end
	
		while true do
				wait(30)
				additionXP()
				print("XP given!")
		if exp.Value >= maxExp.Value then
			level.Value = level.Value + 1
			exp.Value = 0
			maxExp.Value = maxExp.Value + 100
		end
	end
end)				

I had issues earlier with implementing the continuous loop because I had previous functions not allowing the next function to fire (obvious oversight). However, with a script that is constantly checking for a player’s chat, could it be as well embedded somewhere on this script? Or do I need to make a new script in the ServerScriptService?

Edit: I’m also receiving an error with indexing “Chatted”

You could add this to the script that you use

This is because the example I showed you only works in local scripts

Your script modified: (there may be errors)

game.Players.PlayerAdded:Connect(function(player)
	local level = Instance.new("IntValue", player)
	level.Name = "Level"
	level.Value = 1
	
	local exp = Instance.new("IntValue", level)
	exp.Name = "Current"
	exp.Value = 0
	
	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 100
	
	-- continous loop
	
		local function additionXP()
		   player.Level.Current.Value += 25
		end

local canChat = true
local coolDown = 30 -- default cool down

player.Chatted:Connect(function(message) -- detect when the player chats
   if canChat then
       canChat = false -- disable it for the cool down

        coroutine.wrap(function() -- used to prevent thread yielding 
            wait(coolDown)
            canChat = true -- re enable after the cooldown
        end)()

         print("XP given!")
          additionXP()
		if exp.Value >= maxExp.Value then
			level.Value = level.Value + 1
			exp.Value = 0
			maxExp.Value = maxExp.Value + 100
		end
   end
   end)
end)

Sorry for bad formatting, I’m on mobile right now

1 Like

maybe this would help, try this.
this would add a leaderstats as well, if that would help.

You have to put this in a serverscript under ServerScriptService.

local cooldown = 30
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local exp = Instance.new("NumberValue")
	exp.Name = "exp"
	exp.Parent = leaderstats
local truth = true	
	player.Chatted:Connect(function(msg)
		if truth == true then
			truth = false
			player.leaderstats.exp.Value += string.len(msg)
			wait(cooldown)
			truth = true
		end
	end)
end)```
2 Likes

I decided to use this LocalScript for the chat exp. I’m assuming that I put this LocalScript into the StarterPlayerScripts? Seems the script only fires the first time I chat. After waiting for the coolDown, it doesn’t seem to fire agian.

local player = game:GetService("Players").LocalPlayer
local canChat = true
local coolDown = 10 -- default cool down

player.Chatted:Connect(function(message) -- detect when the player chats
	
	if canChat then
		canChat = false -- disable it for the cool down

		coroutine.wrap(function() -- used to prevent thread yielding 
			wait(coolDown)
			canChat = true -- re enable after the cooldown
		end)
		player.Level.Current.Value = player.Level.Current.Value + 5
	end
end)

Put two “()” afterwards for the coroutine so it could call the function after binding to it

coroutine.wrap(function() -- used to prevent thread yielding 
			wait(coolDown)
			canChat = true -- re enable after the cooldown
		end)()

Be careful with using unprotected local scripts as exploiters could modify them

1 Like

Hmmm… just a genuine doubt, isn’t it better to do it on the serverscript instead of a local script?

if so why?

Yea, exploiters don’t have the ability to modify/see the contents of a server script or anything server exclusive

uhhh, then why is your chatted code based locally? I know different people have different methods of doing things. just a general curiosity as to why u preferred to make it local script, because most of the chatted functions i saw has been fired from the server.

I was giving an example on how to use it and for the second script, I modified their code

1 Like

I tried embedding the code into the already existing Level System block, but it kept messing up things with no returning errors. The LocalScript works fine for now, I’m not concerned with exploiters on my game currently, but in the far future, if it ever becomes a problem then I’ll know where at least one of the problems originate. Thanks ya’ll for the help, I really appreciate it!

1 Like