(You can see it in the far right)
Apparently Studio doesn’t like it when I use the player.Chatted Event on a Normal Script. I have tried putting it in a local script, but as you can see there is Datastore, I also refuse moving the Datastore service cause I hope to make the bot save the questions (Basically a self-learning bot). Anyways back to the problem, What should I do to stop the error?
You CANNOT use local player on a server script. You can only use it in a server script. Or, you must refer the player Instance before connecting the Chatted event.
Do you mean Local Script? Also thanks! (By the way, How do I refer to the player Instance or something like that, I’m new to this cause I’ve never gotten this error.)
Local scripts can access Local Player, but since you wanna do it on the server, you might as well just create a table containing the player instanced and make an event that whenever a new player joins, add them into the table and connect them to the Chatted event. Sounds complicated but you should definitely learn the basics of scripting first.
I think he meant: Create a table (Empty at first) when a player is added, you can either: Get the name and add it to the table, or get the Player ID and add it to the table. THEN connect it to the function.
But it is usually used to create chat commands like to make admins only commands because if you need a script that run only when the player chats you don’t need a table, you need it if you want to check that the player chatting is in the table like to make an authorized people list.
game.Players.PlayerAdded:Connect(function(plr) --you get the player, now why do you need a list if you can use it?
plr.Chatted:Connect(function(msg)
if msg == "Hello" then
--do things here
end
end)
end)
List not needed
local admins = {}
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if table.find(admins, plr.Name) then
if msg == "Hello" then
--do things here
end
end
end)
end)
In this case you need a list because you want to check that the player chatting is an admin, I hope to have explained.
Do not use usernames for an admin system, an admin can change their username and lose their privileges, UserId is more beneficial as it is static and never changes if you change your username
game:GetService("Players") is better to use than game.Players as it ensures you get the service in the event the service is renamed accidentally or if something else called Players was found in game
In this case a message will only matter if it was by an admin, so why are you making a chatted event for everyone and then checking if they are an admin? Check first and then make the event
local Players = game:GetService("Players")
local admins = {}
Players.PlayerAdded:Connect(function(plr)
if table.find(admins, plr.UserId) then
plr.Chatted:Connect(function(msg)
if msg == "Hello" then
--Code
end
end)
end
end)
If there are no more messages needed besides Hello, then guard clauses would be good to use in this case