Hello there!
I have door that are opened by saying “open” in chat, but I want them to be openable only by certain players. How can I modify this script to do that?
Script:
local message = "open"
local door = script.Parent
function onChatted(msg, recipient, speaker)
local source = string.lower(speaker.Name)
msg = string.lower(msg)
if (msg == message) then
door.CanCollide = false
for i = 1, 9 do
door.Transparency = door.Transparency + 0.1
wait()
end
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end
game.Players.PlayerAdded:connect(onPlayerEntered)
Check the source vs the player. I would make a table of usernames for the people that can say it. Then, do
local permittedUsers = {"cheekysquid", "builderman"}
local message = "open"
local door = script.Parent
function onChatted(msg, recipient, speaker)
local source = string.lower(speaker.Name)
msg = string.lower(msg)
if (msg == message) and table.find(permittedUsers, source) > -1 then
door.CanCollide = false
for i = 1, 9 do
door.Transparency = door.Transparency + 0.1
wait()
end
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end
The > -1 part means that, table.find() returns -1 if it cannot find the given value. If it returns a value greater than that, that means the table found it. This is the best method for lots of usernames, because using if/or statements such as KJ did will make your code extra long.
Make sure the usernames in the table are in lowercase!
I suggest doing this only if there are only a few players who need this permission.
local message = "open"
local door = script.Parent
function onChatted(msg, recipient, speaker)
if speaker.Name == "goatmanthe90th" or "KJry_s" then
local source = string.lower(speaker.Name)
msg = string.lower(msg)
if (msg == message) then
door.CanCollide = false
for i = 1, 9 do
door.Transparency = door.Transparency + 0.1
wait()
end
end
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end
game.Players.PlayerAdded:connect(onPlayerEntered)