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

Chatted event help?

Asked by 10 years ago

So im making a sort of command, and im wondering how I can take a part of a chatted message, and do code based on that.

Ex.

/timer 100

It does a countdown for 100

Im fairly certain it uses something like msg:sub() or something.

A wiki link would be awesome, or just a little code that tells me what it would look like would also be great!

2 answers

Log in to vote
0
Answered by 10 years ago

Here's the "little code" or a starter code:

--You need an event

game.Players.LocalPlayer.Chatted:connect(function(player)
    -- Code here
end)

Here:s a link to what I think would suit you :D http://wiki.roblox.com/index.php?title=Chat_commands_(temporary)

Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

I always used a server script, not a local script.

game.Players.PlayerAdded:connect(function(plr)
    plr.Chatted:connect(function(msg)
        --code
    end)
end)

Now we have an event firing when a player chats. But how do we know if the first 7 characters of the message is "/timer "? We use :sub().

game.Players.PlayerAdded:connect(function(plr)
    plr.Chatted:connect(function(msg)
        if msg:sub(1,7) == "/timer " then --Remember, space counts as a character!
            local time = tonumber(msg:sub(8)) --Set the length to everything after "/timer ". tonumber() converts a string to a number.
        end
    end)
end)

Now we have checked if they used the correct command and got the length of the time. To actually do the counting down, we will create a new function that creates a hint in workspace.

function countdown(length)
    local h = Instance.new("Hint",workspace)
    for i = length,1,-1 do
        h.Text = tostring(i) --tostring() converts a number to a string.
        wait(1)
    end
    h:Destroy()
end

Now we have our countdown function. The last thing to do is to put it all together!

function countdown(length)
    local h = Instance.new("Hint",workspace)
    for i = length,1,-1 do
        h.Text = tostring(i) --tostring() converts a number to a string.
        wait(1)
    end
    h:Destroy()
end


game.Players.PlayerAdded:connect(function(plr)
    plr.Chatted:connect(function(msg)
        if msg:sub(1,7) == "/timer " then
            local time = tonumber(msg:sub(8)) 
            countdown(time)
        end
    end)
end)

Hope I helped!

0
All I needed was to see how to get the second part, but thanks! Tempestatem 884 — 10y

Answer this question