Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I get a specific part of a chat message using a table?

Sorry for the horrible code.

I want the game to listen out for objects stored in my serverstorage (table), and when someone calls it out it spawns it in game. At the moment it prints twice and it's always nil.

local chat = game:GetService("Chat")
adjectives = {""}
objects = game.ServerStorage:GetChildren()
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		player.Chatted:Connect(function(msg)
			for _,v in ipairs(objects) do
				local cool = table.find(objects, msg)
				print(cool, "|" ..  msg)
				if msg == cool then
					Instance.new("Part",game.Workspace)
				end


			--local clone = msg:Clone()
			--clone.Parent = game.Workspace
			--clone.Position = humanoidRootPart.Position
		
				
		end
	end)
end)
end)

my explorer/output:

https://i.imgur.com/iXPVoNx.png

thanks!

4 answers

Try changing local cool = table.find(objects, msg) to local cool = game.ServerStorage:FindFirstChild(msg)

local chat = game:GetService("Chat")
adjectives = {""}
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(character)
			local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
			player.Chatted:Connect(function(msg)
				local cool = ServerStorage:FindFirstChild(msg)
				print(cool, "|" ..  msg)
				if cool ~= nil then
					Instance.new("Part", workspace)
				end
				
--				local clone = cool:Clone()
--				clone.Parent = game.Workspace
--				clone.Position = humanoidRootPart.Position
			end)
		end)
end)

'ServerScriptService' and 'ServerStorage' seems to be fine.

I see you're up to something.

When i was making 'stand on part or click to popup a TextLabel', i changed the time and it worked finally perfectly!

So if you are up to it put 'something' like wait (0.5) in it!

1 comment

0

didn't seem to work. thanks though!

Try doing this.

local chat = game:GetService("Chat")
adjectives = {""}
objects = game.ServerStorage:GetChildren()
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
        player.Chatted:Connect(function(msg)
            for i,v in pairs(objects) do
                if msg == objects[v] then
                 print(objects[v].."|" ..msg)
                    Instance.new("Part",game.Workspace)
                 else
                 print("No Match")
                end


            --local clone = msg:Clone()
            --clone.Parent = game.Workspace
            --clone.Position = humanoidRootPart.Position


        end
    end)
end)
end)

I had this exact issue a few years ago. Try replacing table.find(objects, msg) with table.find(objects, msg:gsub("\r", "")).

Though, as someone else already answered to this post – I recommend using :FindFirstChild() instead of using a table.

This discussion is preserved as it was. New answers, comments, and votes are closed.