Hi, I’m making a feature for admins where they type in chat
/a Hi this is an announcement
and then players near the admin will receive a gui that shows the announcement text.
I’m having trouble because it’s not working
--Event Variable
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local announceevent = Instance.new("RemoteFunction")
announceevent.Name = "AnnounceEvent"
announceevent.Parent = ReplicatedStorage
--
game.Players.PlayerAdded:connect(function(p)
p.Chatted:connect(function(message)
--Detection
if string.sub(message,1,3) == "/a " then
if p.Name == "ZacBytes" then
--Triggered
local atext = string.sub(message,4)
print(atext)
--Call Event
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character and player:DistanceFromCharacter(p.Character.Head.Position) < 100 then
announceevent:InvokeClient(player,atext)
end
end
--
end
end
end)
end)
This isn’t going to be a helpful reply, but for security reasons I’d recommend not using InvokeClient(), as malicious players can hang the server by tampering with their LocalScripts.
On topic, though, you should change
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character and player:DistanceFromCharacter(p.Character.Head.Position) < 100 then
announceevent:InvokeClient(player,atext)
end
end
to run in a coroutine or spawn. The way you have it set will yield for every player it finds within 100 studs. You could do something like this;
for _, player in pairs(game.Players:GetPlayers()) do
coroutine.resume(coroutine.create(function()
if player.Character and player:DistanceFromCharacter(p.Character.Head.Position) < 100 then
announceevent:InvokeClient(player,atext)
end
end))
end
The code bound to announceevent’s OnClientInvoke property is what is erroring, and not the code you provided
I recommend to find the solution, you use prints to find out what exactly the value of message is, and also to take a look at your client-side code (put it on here too so we can help).
Selecting and quoting is broken for me right now so I’ll mention you instead @colbert2677
Using “player.Name” as a player check (use UserId instead, or Players::GetUserIdFromNameAsync)
To explain, the issue with using names is that players can change their names. If you change your name, you’ll need to go into this code and update it manually – if you used your user ID you’d never need to think about it again. Another player could get admin permissions if they had your old name and you didn’t update the script, either. For this reason, :GetUserIdFromNameAsync() isn’t a valid solution if this correct player identification is what you’re going for, as it still relies on the player’s name.
As for actually implementing user ID, it’s pretty straightforward and similar to using names. If you don’t how, you can easily get your user ID to put in a script like this by going to your profile page and copying the number in the URL: https://www.roblox.com/users/000000/profile.
Running this Chatted function for every single player that chats
Putting your player check in after the Chatted event fires and the message is processed
Yep, the solution to this here is vaguely implied so I’ll clarify it – the solution to this would be to check who the player is before you make the connection instead of within it. So if a player is the game owner, listen for the Chatted event, instead of the other way around.
Also, what are pre-post measures? You mean the issues with the thread that need to be fixed?
A player can’t use your old username. The only time you’d need to account for player usernames is if a Microsoft user for XBox One had a gamertag matching your name. As well, the function accepts old usernames. Put “colbert2677” as an argument (my current name) and you’ll get 6809102. Put “SentientCrow” (one of my past usernames) and you’ll get 6809102. A username gets permanently associated with a UserId, whether that’s from a sign up or bought username change.
@Darkmist101 is referring to the poor indentation (your ends are all over the place), Try print()ing where you need to figure out at what point the message stops going through. Is this the exact same code you have in Studio?