I’m attempting to make a code in which will listen to the speaker and will turn off collision from the part that they stated (for instance, “!open 1” - in which will set the CanCollide value to false in game.Workspace.door1), but it doesn’t seem to go through it as expected. What’s the concurrent issue here?
--[ Variables
local doors = {game.Workspace.door1, game.Workspace.door2, game.Workspace.door3, game.Workspace.door4}
local prefix = "!"
--[ Setup
game.Players.PlayerAdded:Connect(function(plr)
if plr:GetRankInGroup(3788654) >= 254 then
plr.Chatted:Connect(function(msg)
for _, i in ipairs(doors) do
local msg = string.lower(msg)
local splitMsg = msg:split()
local arg1 = splitMsg[1]
local cmd = arg1:split(prefix)[2]
local doorNumber = splitMsg[2]
doorNumber = tonumber(doorNumber)
-- code breaks underneath. when I tried to print out doorNumber or splitMsg[2], it came across as "nil"
if (cmd == "open") and i:lower():sub(5, #doorNumber) == doorNumber then
i.CanCollide = false
i.Sound:Play()
elseif (cmd == "close") and i:lower():sub(5, #doorNumber) == doorNumber then
i.CanCollide = true
i.Sound:Play()
end
end
end)
end
end)
I realised for the variable, local splitMsg = msg:split(), needed a space, therefore I changed it to local splitMsg = msg:split(" "), and also removed doorNumber = tonumber(doorNumber) as it was unnecessary, but the code still breaks underneath.
Are you sure you are doing the command handling itself properly? For starters you should probably be checking the message outside of the for loop. There really isnt any reason to check the message more than once. I would suggest adding prints. I did a little test myself and this was the output.
first arg: !open 1 command: open 1 door number: nil
Upon typing !open 1, it does not appear to handle the message typed in correctly.
code used to test:
game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
local msg = string.lower(Message)
local splitMsg = msg:split()
local arg1 = splitMsg[1]
local cmd = arg1:split("!")[2]
local doorNumber = splitMsg[2]
print("first arg " .. tostring(arg1),"\n command " .. tostring(cmd), "\n door number " .. tostring(doorNumber))
end)
end)
`
Edit: I see your updated code and it came out to work properly.
Alright so it seems like you were complicating the code a little bit, ideally you should only have to do something like this:
game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
local msg = string.lower(Message)
local splitMsg = msg:split(" ")
local arg1 = splitMsg[1]
local cmd = arg1:split("!")[2]
local doorNumber = splitMsg[2]
print("first arg " .. tostring(arg1),"\n command " .. tostring(cmd), "\n door number " .. tostring(doorNumber))
local Door = WhereEverYourDoorsAre:FindFirstChild("Door" .. doorNumber)
if cmd:lower() == "open" then
-- Door handling stuff
elseif cmd:lower() == "close" then
-- Door handling stuff
end
end)
end)