Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago

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

Log in to vote
1
Answered by 1 year ago

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)
Ad
Log in to vote
0
Answered by 1 year ago

'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!

0
didn't seem to work. thanks though! kek_productions 7 — 1y
Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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)

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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.

Answer this question