Commands

From Goodblox Wiki
Revision as of 15:49, 22 June 2020 by Pizzaboxer (talk | contribs) (Created page with "{{CatUp|Tutorials}} __TOC__ == How to make commands == This is more of a scripting tutorial. == Introduction == This tutorial teaches you how to use <code>function onChatt...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

How to make commands

This is more of a scripting tutorial.

Introduction

This tutorial teaches you how to use function onChatted.
The scripts below only work on script builder.

Beginning

First you need to be a good enough scripter or else you can't understand the code below.
The first line is the person you want to have commands.

vip = game.Players.[your name here]

Next you put this line in. This line makes a function that uses your chatted messages.

function onChatted(msg)

Now we need to put what message we need to say to activate the command. To do that use this line:

if msg == "hi" then
local m = Instance.new("Message")
m.Parent = game.Workspace
m.Text = "hi"
wait(2)
m:remove()
end

Theres no limit of how many commands you can make. After your done put this on the very end of your script. This connects your voice to the script so the script can work.

end
vip.Chatted:connect(onChatted)

The finished script

Here are the finished commands script we made so far.

vip = game.Players.[your name here]

function onChatted(msg)
   if msg == "hi" then
      local m = Instance.new("Message")
      m.Parent = game.Workspace
      m.Text = "hi"
      wait(2)
      m:remove()
   end
end

vip.Chatted:connect(onChatted)

There are some problems here. The script only works in a script builder. To enable it in your place you have to use function onPlayerAdded which that may come later. TODO: Write about function onPlayerAdded

Advanced

Lets say that you want to modify a value of something. You could keep putting if msg == "do/1" then ... if msg == "do/2" then but that wont work good. Its time to learn about string.sub. To do this, examine this block of code:

if (string.sub(msg,1,3) == "do/") then
a = string.sub(msg,4)
end

So when you say do/7, a will be set to 7. Lets take a look at this string.sub.

if (string.sub(msg,1,3) == "do/") then

In this line you see three numbers. The third one is what you need. Count the letters on the string on the right.

a = string.sub(msg,4)

Same line here. But all you need is to put one number up from the previous line.

Expert

function onChatted(msg, recipient, speaker) is a more complicated technique of the onChatted function. It's useful for when you're trying to refer to or modify the "speaker", which is the person who is chatting the message. The example I'm going to set is the commonly used talky regen script.

model = game.Workspace.ModelName -- Name of the model you're going to regenerate
backup = model:clone() -- makes a backup copy of your model, which you will use later

function onChatted(msg, recipient, speaker) 

local source = string.lower(speaker.Name) -- Converts name of speaker to lowercase
msg = string.lower(msg) -- This will make the script non-caps sensitive. "regen" or "Regen" or "rEgEN" will all work.

if (msg == "regen") then -- Word you have to say to regen; message can NOT have caps or spaces

model:remove()
model = backup:clone() 
model.Parent = game.Workspace 
model:makeJoints() 
end 

end 

-- The content below is absolutely necessary for this script; don't delete it or it won't work.

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered)

FAQs

Are you having trouble making this script work?

Hint: Try deleting your model with the laser in online mode first, then using the regen script with the "regen" keyword.

Suicide and Team Change

This script will make use of the argument known as "speaker". This script is basically a secondary form of the Reset tool. When the speaker says "suicide", he will respawn.

function onChatted(msg, speaker)
    
    source = string.lower(speaker.Name)
    msg = string.lower(msg)
    -- Note: This one is NOT caps sensitive

    if msg == "suicide" then
        speaker.Character.Head:remove() -- The name says it all; the character dies.
    end
end

function onPlayerEntered(newPlayer)
        newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end) 
end
 
game.Players.ChildAdded:connect(onPlayerEntered)

With this in mind, you can improve the script even more. This script is a talk-to-join script. Simply say "join reds" or "join blues" to switch teams. This is the same script used in Ultimate Paintball CTF by miked.

Tip: You can't change teams while still standing on a SpawnLocation from another team with this script. You'll just die, and regenerate as the same color.

function onChatted(msg, speaker)
    

    --you need capitals
    
    source = string.lower(speaker.Name)
    
    --the below "if then" statement is necessary for this script.
    if string.sub(msg, 1, 4) == "join" then
        local team = game.Teams:findFirstChild(string.sub(msg, 6))
        if team ~= nil then
            speaker.TeamColor = team.TeamColor
            wait(0.1)
            speaker.Character.Head:Remove()
        end
    end

    if msg == "join reds" then
        speaker.TeamColor = BrickColor.new(21) --Changes speaker's team color to red
        speaker.Character.Head:remove() --Kills the player to make the switch

    elseif msg == "join blues" then
        speaker.TeamColor = BrickColor.new(23) --Changes speaker's team color to blue
        speaker.Character.Head:remove() --Kills the player to make the switch
    end
end

--Again, you need this last part.

function onPlayerEntered(newPlayer)
        newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end) 
end
 
game.Players.ChildAdded:connect(onPlayerEntered)

As you can see, there are many uses for the new variables. As you just learned, you can use them to modify the person who's chatting the message (such as team color). This isn't the only use for the variables; use this guide to make your own unique scripts using the variables of speaker, recipient, and msg. How you can use them is up to you. If you have excellent scripting knowledge, you'll be able to make good use from the variables.

Adding pre-existing Command Scripts

Introduction

The Commands script is used to allow for voice commands for various things, such as making random explosions, or changing the time of day.

Script one

function onChatted(msg, recipient, speaker)

	-- convert to all lower case

	local source = string.lower(speaker.Name)
	msg = string.lower(msg)

	
	if (msg == "day") then
		game.Lighting.TimeOfDay = "16:32:00"
	end
	

	if (msg == "night") then
		game.Lighting.TimeOfDay = "00:00:00"
	end

	-- Below is only for me
	if (source ~= "lavamaster") then return end

	if (msg == "deadly haze") then
		local m = Instance.new("Message")
		m.Text = "A mysterious haze is floating towards us..."
		m.Parent = game.Workspace
		wait(4)
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "lavamaster") then
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character.Humanoid.Health = 0
				end
			end
		end
		wait(2)
		m.Parent = nil
	end

	if (msg == "boom!") then
		local m = Instance.new("Message")
		m.Text = "We're being bombed!"
		m.Parent = game.Workspace
		wait(4)
		for i=1,100 do
			local x = math.random(-200,200)
			local z = math.random(-200,200)
			local y = math.random(0,40)

			local r = math.random(6,16)

			local ex = Instance.new("Explosion")
			ex.Position = Vector3.new(x,y,z)
			ex.BlastRadius = r
			ex.Parent = game.Workspace
			wait(.05)
		end
		wait(2)
		m.Parent = nil
	end


end

function onPlayerEntered(newPlayer)
	newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end

game.Players.ChildAdded:connect(onPlayerEntered)

Script two

function onChatted(msg, recipient, speaker)

	-- convert to all lower case

	local source = string.lower(speaker.Name)
	msg = string.lower(msg)

	
	if (msg == "day") then
		game.Lighting.TimeOfDay = "14:00:00"
	end
	

	if (msg == "night") then
		game.Lighting.TimeOfDay = "00:00:00"
	end

	if (msg == "normal") then

		game.Lighting.TimeOfDay = "0:00:00"
		game.Lighting.TopAmbientV9 = Color3.new(215,214,223)
		game.Lighting.SpotLightV9 = Color3.new(197,197,197)
		game.Lighting.BottomAmbientV9 = Color3.new(122,140,120)
		game.Lighting.TimeOfDay = "14:00:00"

	end

	if (msg == "neon") then

		game.Lighting.TopAmbientV9 = Color3.new(0,255,255)
		game.Lighting.SpotLightV9 = Color3.new(0,255,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,255)

	end

	if (msg == "red") then

		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)

	end

	if (msg == "yellow") then

		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)

	end

	if (msg == "green") then

		game.Lighting.TopAmbientV9 = Color3.new(0,255,0)
		game.Lighting.SpotLightV9 = Color3.new(0,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,0)

	end

	if (msg == "darkblue") then

		game.Lighting.TopAmbientV9 = Color3.new(0,0,255)
		game.Lighting.SpotLightV9 = Color3.new(0,0,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,0,255)

	end

	if (msg == "disco!") then

		game.Lighting.TopAmbientV9 = Color3.new(0,0,255)
		game.Lighting.SpotLightV9 = Color3.new(0,0,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,0,255)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(0,255,0)
		game.Lighting.SpotLightV9 = Color3.new(0,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(0,255,255)
		game.Lighting.SpotLightV9 = Color3.new(0,255,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,255)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(0,0,255)
		game.Lighting.SpotLightV9 = Color3.new(0,0,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,0,255)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(0,255,0)
		game.Lighting.SpotLightV9 = Color3.new(0,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(0,255,255)
		game.Lighting.SpotLightV9 = Color3.new(0,255,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,255)
		wait(0.5)
		game.Lighting.TimeOfDay = "0:00:00"
		game.Lighting.TopAmbientV9 = Color3.new(215,214,223)
		game.Lighting.SpotLightV9 = Color3.new(197,197,197)
		game.Lighting.BottomAmbientV9 = Color3.new(122,140,120)
		game.Lighting.TimeOfDay = "14:00:00"
	end

	if (source ~= "username") then return end--Change this to your name.

	if (msg == "die") then
		local m = Instance.new("Message")
		m.Text = "EVERYONE IS GONNA DIE!!!!!!!!!!"
		m.Parent = game.Workspace
		wait(4)
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "username") then
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character.Humanoid.Health = 0
				end
			end
		end
		wait(2)
		m.Parent = nil
	end

	if (msg == "blackout") then

		game.Lighting.TopAmbientV9 = Color3.new(0,0,0)
		game.Lighting.SpotLightV9 = Color3.new(0,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(0,0,0)

	end



	if (msg == "listen") then

		local m = Instance.new("Message")
		m.Text = "EVERYONE STOP TALKING!!!!"
		m.Parent = game.Workspace
		wait(4)
		m.Parent = nil

	end



end

function onPlayerEntered(newPlayer)
	newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end

game.Players.ChildAdded:connect(onPlayerEntered)

Script three

function onChatted(msg, recipient, speaker)

	local source = string.lower(speaker.Name)
	msg = string.lower(msg)

	
	if (msg == "day") then
		game.Lighting.TimeOfDay = "14:00:00"
	end
	

	if (msg == "night") then
		game.Lighting.TimeOfDay = "00:00:00"
	end

	if (msg == "normal") then

		game.Lighting.TimeOfDay = "0:00:00"
		game.Lighting.TopAmbientV9 = Color3.new(215,214,223)
		game.Lighting.SpotLightV9 = Color3.new(197,197,197)
		game.Lighting.BottomAmbientV9 = Color3.new(122,140,120)
		game.Lighting.TimeOfDay = "14:00:00"

	end

	if (msg == "neon") then

		game.Lighting.TopAmbientV9 = Color3.new(0,255,255)
		game.Lighting.SpotLightV9 = Color3.new(0,255,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,255)

	end

	if (msg == "red") then

		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)

	end

	if (msg == "yellow") then

		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)

	end

	if (msg == "green") then

		game.Lighting.TopAmbientV9 = Color3.new(0,255,0)
		game.Lighting.SpotLightV9 = Color3.new(0,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(0,255,0)

	end

	if (msg == "blue") then

		game.Lighting.TopAmbientV9 = Color3.new(0,0,255)
		game.Lighting.SpotLightV9 = Color3.new(0,0,255)
		game.Lighting.BottomAmbientV9 = Color3.new(0,0,255)

	end

	if (msg == "black") then

		game.Lighting.TimeOfDay = "00:00:00"
		game.Lighting.TopAmbientV9 = Color3.new(0,0,0)
		game.Lighting.SpotLightV9 = Color3.new(0,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(0,0,0)

	end

	if (msg == "white") then

		game.Lighting.TimeOfDay = "00:00:00"
		game.Lighting.TopAmbientV9 = Color3.new(255,255,255)
		game.Lighting.SpotLightV9 = Color3.new(255,255,255)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,255)

	end

	-- Below is the admin commands.
	if (source ~= "username") then return end --Change username to your name.

	if (msg == "die") then --Change this if you want.
		local m = Instance.new("Message")
		m.Text = "Test message"--Change this if you want.
		m.Parent = game.Workspace
		wait(4)
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "username") then --change username to your name
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character.Humanoid.Health = 0
				end
			end
		end
		wait(2)
		m.Parent = nil
	end

	if (msg == "peoplewhite") then
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= username") then --Change username to your name.
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character:FindFirstChild("Head").BrickColor = BrickColor.new(1)
					c[i].Character:FindFirstChild("Torso").BrickColor = BrickColor.new(1)
					c[i].Character:FindFirstChild("Left Arm").BrickColor = BrickColor.new(1)
					c[i].Character:FindFirstChild("Right Arm").BrickColor = BrickColor.new(1)
					c[i].Character:FindFirstChild("Left Leg").BrickColor = BrickColor.new(1)
					c[i].Character:FindFirstChild("Right Leg").BrickColor = BrickColor.new(1)

				end
			end
		end
	end

	if (msg == "freeze") then
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "username") then --Change username to your name.
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character:findFirstChild("Torso").Anchored = true

				end
			end
		end
	end

	if (msg == "thaw") then
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "username") then --Change username to your name.
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character:FindFirstChild("Torso").Anchored = false

				end
			end
		end
	end


	if (msg == "savagebeating") then --You may want to change this.
		local m = Instance.new("Message")
		m.Text = "Test message 1234"--Change this.
		m.Parent = game.Workspace
		wait(4)
		local c = game.Players:Gethildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "username") then --Change username to your name.
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character.Humanoid.Health = 1
				end
			end
		end
		wait(2)
		m.Parent = nil
	end

	if (msg == "zeekyboogydoog") then--Change this to boom! or somthing if you want too.
		local m = Instance.new("Message")
		m.Text = "Test Message 4567"--You may want to change this
		m.Parent = game.Workspace
		wait(4)
		for i=1,100 do
			local x = math.random(17.5,76.5)
			local y = math.random(0,41.6)
			local z = math.random(-115,101)

			local r = math.random(6,16)

			local ex = Instance.new("Explosion")
			ex.Position = Vector3.new(x,y,z)
			ex.BlastRadius = r
			ex.Parent = game.Workspace
			wait(.05)
		end
		wait(2)
		m.Parent = nil
	end

	if (msg == "brandonwantbigboom") then
		local m = Instance.new("Message")
		m.Text = "Test Message 7890"
		m.Parent = game.Workspace
		wait(4)


		local ex = Instance.new("Explosion")
		ex.Position = Vector3.new(29.5,0.2,-7)--Change these numbers or the explosion might be somewhere unuseful.
		ex.BlastRadius = 300
		ex.Parent = game.Workspace
		m.Parent = nil
	end

	if (msg == "listen") then

		local m = Instance.new("Message")
		m.Text = "EVERYONE STOP TALKING!!!!"
		m.Parent = game.Workspace
		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,0,0)
		game.Lighting.SpotLightV9 = Color3.new(255,0,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,0,0)
		wait(0.5)
		game.Lighting.TopAmbientV9 = Color3.new(255,255,0)
		game.Lighting.SpotLightV9 = Color3.new(255,255,0)
		game.Lighting.BottomAmbientV9 = Color3.new(255,255,0)
		wait(0.5)
		game.Lighting.TimeOfDay = "0:00:00"
		game.Lighting.TopAmbientV9 = Color3.new(215,214,223)
		game.Lighting.SpotLightV9 = Color3.new(197,197,197)
		game.Lighting.BottomAmbientV9 = Color3.new(122,140,120)
		game.Lighting.TimeOfDay = "14:00:00"
		m.Parent = nil
	end
end

function onPlayerEntered(newPlayer)
	newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end

game.Players.ChildAdded:connect(onPlayerEntered)


If you want to "learn" these commands, then here you go.

if (msg == "freeze") then
		local c = game.Players:GetChildren()
		for i=1,#c do
			if (string.lower(c[i].Name) ~= "username") then --Change username to your name.
				if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then
					c[i].Character:FindFirstChild("Torso").Anchored = true

This "freezes" players, by anchoring the torso.


if (string.lower(c[i].Name) ~= "username")

This finds the name of the character.


if (c[i].Character ~= nil and c[i].Character.Humanoid ~= nil) then

This gets the character...


c[i].Character:FindFirstChild("Torso").Anchored = true

And this anchors that character's torso.

The "thaw" works the same, but instead, "Unanchors" the torso of the character.