Chat script not working yet no errors?

So I have this in a server script. I want to be where they type :open in the chat, it opens this part named “Door.” Except they have to be above the group rank 10.

It doesn’t work, but has no errors? I just started learning lua so there’s probably something I did wrong. I accidentally hit enter but forgot to add something. Give me a second to fix the script.

local Players = game:GetService("Players")
local GROUP_ID = 5560221
if Player:GetRankInGroup(<10) then
onChatted(msg, speaker)
 local source = string.lower(speaker.Name)
 msg = string.lower(msg)
 if msg == ":open" then
     local door = workspace:FindFirstChild("Door")
     local function open()
         door.CanCollide = true
         door.Transparency = 1
     end
 end
end)```
local function open(door) 
	door.CanCollide = true 
	door.Transparency = 1 
end 

function onChatted(msg, speaker) 
	local source = string.lower(speaker.Name) 
	msg = string.lower(msg) 
	if msg == ":open" then 
		local door = workspace:FindFirstChild("Door") 
		open(door)
	end
end)

This should fix your problem

1 Like

This may sound a bit silly, but are you actually firing the onChatted event? If not, you’ll need to fire it with the event, which I believe in this instance is game.Players.Chatted:Connect(onChatted(msg, speaker))

So I have to create a remote event in server storage?

Are you sure there are no errors?
Because there’s no way the line:

if Player:GetRankInGroup(<10) then

will work without errors.

It should be

if Player:GetRankInGroup(GROUP_ID) < 10 then

Additionally, you can’t just do onChatted. You have to hook up to a player.

So your fixed code becomes:

local Players = game:GetService("Players")
local GROUP_ID = 5560221
local door = workspace:WaitForChild("Door")
local function open()
    door.CanCollide = true
    door.Transparency = 1
end
Players.PlayedAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if plr:GetRankInGroup(GROUP_ID) < 10 then
           msg = string.lower(msg)
           if msg == ":open" then
              open()
           end
        end
    end)
end)

What this does is when a player is added, it hooks up a function which hooks up a function to the on chatted event of the new player. If they have the right rank and typed the correct message, it will open the door.

@Chordily
There is NO need for remote events. The server has all of the information it needs on it, and the client doesn’t need to send information.


@Benified4Life
The code you provided will not work, as game.Players.Chatted is not a valid event.

Also, that’s not how you do events. It would be

Event:Connect(func)

Instead of

Event:Connect(func(parameter1))

Where Event is the event, func is the function, and parameter1 is the parameter.

For more info: https://developer.roblox.com/en-us/articles/events

Still not working. No errors in the output, but nope.

Try to use print if the part of the script is working.

What do you mean? Sorry, newbie here.

Try this. It’s more simple. :slight_smile:

local GROUP_ID = 5560221
local door = workspace:WaitForChild("Door")
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(msg)
if msg:sub(1,5)== ":open" then
	if Player:GetRankInGroup(GROUP_ID) > 10 then
	door.CanCollide = true
	door.Transparency = 1
	end
end
end)
end)

Tell me if it works.

Printing certain parts of a script is a method used to test if certain parts of script are running. It’s usually used when Output isn’t showing anything.

Example:

local Example = 1

function CreatePart(NameofPart)
local New = Instance.New("Part")
New.Name = NameofPart
New.Parent = workspace
end

if Example == 1 then CreatePart("One")
elseif Example == 2 then CreatePart("Two")
elseif Example == 3 then CreatePart("Three")
elseif Example == 4 then CreatePart("Four")
elseif Example == 5 then CreatePart("Five")
end
-- Missing an end.

If Output wasn’t showing any errors with this script you could try:

local Example = 1

function CreatePart(NameofPart)
print("Spawn Function Success")
local New = Instance.New("Part")
New.Name = NameofPart
New.Parent = workspace
end

if Example == 1 then CreatePart("One")
elseif Example == 2 then CreatePart("Two")
elseif Example == 3 then CreatePart("Three")
elseif Example == 4 then CreatePart("Four")
elseif Example == 5 then CreatePart("Five")
end
-- Missing an end.

By doing this, you’ll see that Spawn Function Success won’t print, indicating that there’s something wrong with the elseif chain.

It works. Much appreciated! Cheers!