Code Snippets: Difference between revisions

From Goodblox Wiki
Jump to navigationJump to search
(Created page with "__TOC__ ==Introduction== These are published articles to help you understand how certain creations in Roblox work and how they where developed. It is not a source for free s...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
__TOC__
==Introduction==
Below are common codes used in scripts. You can use these in your scripts, or to learn from them.
 
==Random Number==
Generates a random number between 3 and 9 inclusively:
math.random(3,9)
 
==Create an Explosion==
The following function creates an explosion at a given position in 3D space.
 
function createExplosion(position)
  explosion = Instance.new("Explosion")--Creates The Explosion.
  explosion.Position = position --Where The Explosion Is.
  explosion.Parent = game.Workspace --Puts the explosion in the game.
end
 
To create an explosion at (0, 100, 0), for example, you would do:
 
createExplosion(Vector3.new(0,100,0))
 
==Create a Deadly brick==
The following script kills any humanoid that touches it.
 
function onTouched(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
        --Basically this just checks to see if it is a real player touching this brick.
if (human ~= nil) then --If it is a real player, then DESTROY THEM!
human.Health = 0 --Your Health Is Now 0.
end
end
if (script.Parent ~= nil) and (script.Parent.className == "Part") then --Work if in a block
connection = script.Parent.Touched:connect(onTouched)
end
 
==Color Changing==
Changes colors indefinitely.
 
while true do
  script.Parent.Color = Color3.new(math.random(), math.random(), math.random())
  wait(0.5) --Waits 1/2 a second till swapping to a random color.
end
 
==Healing Potion==
Ready-to-go script. Add it onto a block, and it activates on touch.
 
power = 10 --How fast you want to heal people.
function onTouched(part)
  local h = part.Parent:FindFirstChild("Humanoid")
  if h~=nil then
      h.Health = h.Health + power
  end
end
script.Parent.Touched:connect(onTouched) --If touched, then run.
 
==Regeneration==
You can use this script, just put it into the Workspace, not a brick or model. Just change the "MyModelName" with the name of your model.
 
model = game.Workspace.MyModelName --Replace the 'MyModelName' with the name of your model.
messageText = "Regenerating MyModelName..." --And replace it here!
message = Instance.new("Message")--Make a Message
message.Text = messageText
backup = model:clone()--Got to Make A Back-Up.
while true do --This Makes It Continuous
wait(300) --How long till regeneration.
message.Parent = game.Workspace --The Message Should Show To Everyone
model:remove()
wait(4) -- display regen message for 4 seconds
model = backup:clone() --Clone the Back-up To Make Another One
model.Parent = game.Workspace --You Got To See It Right?
model:makeJoints() --Make Sure It Doesn't Fall Apart.
message.Parent = nil --clears message
end
 
==Regeneration-In-Model Script==
*'''Name''' Regeneration-In-Model Script
*'''Type''' You put this regeneration script inside a MODEL, NOT the workspace.
*'''Author''' Furnace
model = script.Parent
backup = model:Clone()
regentime = 300 --5 minutes
message = Instance.new("Message")
message.Text = "Regenerating ".. model.Name.. "..."
if (model ~= game.Workspace) then
  while true do
    wait(regentime)
    m = message:Clone()
    m.Parent = game.Workspace
    wait(1)
    model:remove()
    model = backup:Clone()
    wait(3)
    model.Parent = game.Workspace
    model:MakeJoints()
    m.Parent = nil
  end
end
 
==Anti-Flying Script==
*'''Name''' Anti-flying script
*'''Type''' Cheating prevention script for those who hate flyers.
*'''Author''' Gamer3D
*'''Description''' This script blasts flyers out of the sky. When it is placed in a brick to protect and it's sensitivity is adjusted, it will kill flyers, and leave plain loiterers alone.
*'''''This script is outdated, you can no longer use the flying bug.'''''
*'''Code'''
tracked = nil -- Keep track of checked players so we don't blow up plain loiterers
trackedPosition = nil
sensitivity = 1 -- 1 is normal. Higher values indicate higher sensitivity
function checkIfFlyer(player, brick, slot)
local relativePos = brick.Position - player.Character.PrimaryPart.Position
if (relativePos.magnitude < 4 * sensitivity) then -- Brick is within normal distance for flying
if (relativePos.y > -2 * sensitivity) and (relativePos.y < sensitivity) then -- Brick is at a good height for flying
if tracked = player then
local moveDist = player.Character.PrimaryPart.Position - trackedPosition
if (moveDist.magnitude * sensitivity > 8) then -- Player moved away, but kept brick. Almost certainly a flyer.
return true
end
end
tracked = player
trackedPosition = player.PrimaryPart.Position
end
end
return false
end
function makeExplosion(Pos) -- Blow up
local myExplosion = Instance.new("Explosion")
myExplosion.Position = Pos
myExplosion.Parent = game.Workspace
end
-- Main loop
playerList = game.Players:children()
brickList = nil
while true do
if (script.Parent.className == "Part") then
for i = 1, #playerList do
if checkIfFlyer(playerList[i], script.Parent, 1) then
wait(0.5)
makeExplosion(script.Parent.Position)
end
end
playerList = game.Players:children() -- Keep track of new players too
end
if (#PlayerList == 0) then -- No sense bogging anything down.
wait(10)
end
if (#playerList > 0) then
wait(2)
end
end


==Introduction==
==Talky Regeneration==
*'''Name''' TalkyRegenScript
*'''Type''' Regens a model
*'''Author''' matrix35
*'''Description''' Regens a model by saying a word
*'''Notes''' (in script)
*'''Code'''
 
model = game.Workspace.Model -- The model to regenerate. Change Model to the models name
backup = model:clone() -- clones the model
function onChatted(msg, recipient, speaker)
local source = string.lower(speaker.Name)
msg = string.lower(msg)
if (msg == "regen") then -- The word you want people to say to regen it. change it
model:remove() --removes the model
model = backup:clone() --clones the model
model.Parent = game.Workspace -- makes the parent the Workspac
model:makeJoints() -- generates the model
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end)
end
game.Players.ChildAdded:connect(onPlayerEntered)
-- When you are done editing this, save this place then go to it then say the word to regenerate the model.
 
==Turn all your bricks a certain color==
*'''Name''' Unknown
*'''Type'''
*'''Author''' Unknown
*'''Description'''
*'''Notes''' Put script in Workspace
*'''Code'''
function turnBlack(where)
local ch=where:GetChildren() --note that we need local; see below
local i = 0
for i = 1, #ch do -- one number for each "child"
if #ch[i]:GetChildren()>0 then turnBlack(ch[i]) end -- when to stop this loop
if ch[i].className=="Part" or ch[i].className=="Seat" or ch[i].className=="SpawnLocation" then ch[i].BrickColor=BrickColor.new("Black") end -- what specific objects to change colors (e.g., bricks, not cameras)
end
end
turnBlack(game.Workspace)
 
==What color is this brick?==
*'''Name''' What color is this brick?
*'''Type'''
*'''Author''' Unknown
*'''Description'''
*'''Notes''' Put script in brick
*'''Code'''
function onTouched(hit)
c=script.Parent.BrickColor
if c==BrickColor.new("Bright red") then
print("it's red!")
else
print("It's not red.")
end
end
script.Parent.Touched:connect(onTouched)


These are published articles to help you understand how certain creations in Roblox work and how they where developed. It is not a source for free scripts or models, but rather a way to learn the inner-workings of inventions to help you use the techniques on your own creations! '''These articles require a good amount of knowledge in scripting and the Roblox Studio! They are for advanced readers and thinkers!'''
==Random Messages==
*'''Note:''' This will select a random message from your prewritten group of messages and print it out.
local randommessages = {"Sample message 1",  "Sample message 2", "Sample message 3", "Sample message 4", "Sample message 5", "Sample message 6"} 
local rndmsg = math.random(1, 6)
--Random number between 1, and the number of items in the table 
print(randommessages[rndmsg]) 


It is recommended not to edit these articles unless you are the author. These are complex and are the thought lines of one person. Also, articles should not be made in "second person", so don't use terms like "you".
==Simple Breaking Ice==
When the brick inside gets touched, it has a 3 in 5 chance for breaking for a moment.
*'''Name''' Ice
*'''Type''' Random number usage
*'''Author''' WhoKnows OR main account TheName, WhoKnowsAndTheName on wiki.
*'''Description'''
*'''Notes''' Put script in brick
*'''Code'''
function ont(hit)
hum = hit.Parent:findFirstChild("Humanoid")
if (hum ~= nil) then
mn = math.random(1, 5)
if (mn ~= 3) and (mn ~= 4) and (mn ~= 2) then
print(mn)
else
print(mn)
script.Parent.Transparency =  1
script.Parent.CanCollide = false
wait(0.5)
script.Parent.Transparency = 0.5
script.Parent.CanCollide = true
end
end
end


===Simple Inventions===
script.Parent.Touched:connect(ont)
[[Conceptual Design: How Fountains Work]]


===Vehicles===
[[Category:Scripting Tutorials]]
[[Conceptual Design: How Vehicles Work]]

Latest revision as of 16:26, 22 June 2020

Introduction

Below are common codes used in scripts. You can use these in your scripts, or to learn from them.

Random Number

Generates a random number between 3 and 9 inclusively:

math.random(3,9)

Create an Explosion

The following function creates an explosion at a given position in 3D space.

function createExplosion(position)

  explosion = Instance.new("Explosion")--Creates The Explosion.
  explosion.Position = position --Where The Explosion Is.
  explosion.Parent = game.Workspace --Puts the explosion in the game.

end

To create an explosion at (0, 100, 0), for example, you would do:

createExplosion(Vector3.new(0,100,0))

Create a Deadly brick

The following script kills any humanoid that touches it.

function onTouched(hit)
	local human = hit.Parent:FindFirstChild("Humanoid") 
       --Basically this just checks to see if it is a real player touching this brick.
	if (human ~= nil) then --If it is a real player, then DESTROY THEM!
		human.Health = 0 --Your Health Is Now 0.
	end
end

if (script.Parent ~= nil) and (script.Parent.className == "Part") then --Work if in a block
	connection = script.Parent.Touched:connect(onTouched)
end

Color Changing

Changes colors indefinitely.

while true do
  script.Parent.Color = Color3.new(math.random(), math.random(), math.random())
  wait(0.5) --Waits 1/2 a second till swapping to a random color.
end 

Healing Potion

Ready-to-go script. Add it onto a block, and it activates on touch.

power = 10 --How fast you want to heal people.
function onTouched(part)
  local h = part.Parent:FindFirstChild("Humanoid")
  if h~=nil then
     h.Health = h.Health + power
  end
end

script.Parent.Touched:connect(onTouched) --If touched, then run.

Regeneration

You can use this script, just put it into the Workspace, not a brick or model. Just change the "MyModelName" with the name of your model.

model = game.Workspace.MyModelName --Replace the 'MyModelName' with the name of your model.
messageText = "Regenerating MyModelName..." --And replace it here!
message = Instance.new("Message")--Make a Message
message.Text = messageText
backup = model:clone()--Got to Make A Back-Up.

while true do --This Makes It Continuous
	wait(300) --How long till regeneration.
	message.Parent = game.Workspace --The Message Should Show To Everyone
	model:remove()

	wait(4) -- display regen message for 4 seconds

	model = backup:clone() --Clone the Back-up To Make Another One
	model.Parent = game.Workspace --You Got To See It Right?
	model:makeJoints() --Make Sure It Doesn't Fall Apart.
	message.Parent = nil --clears message
end

Regeneration-In-Model Script

  • Name Regeneration-In-Model Script
  • Type You put this regeneration script inside a MODEL, NOT the workspace.
  • Author Furnace
model = script.Parent
backup = model:Clone()
regentime = 300 --5 minutes
message = Instance.new("Message")
message.Text = "Regenerating ".. model.Name.. "..."
if (model ~= game.Workspace) then
  while true do
   wait(regentime)
   m = message:Clone()
   m.Parent = game.Workspace
   wait(1)
   model:remove()
   model = backup:Clone()
   wait(3)
   model.Parent = game.Workspace
   model:MakeJoints()
   m.Parent = nil
  end
end

Anti-Flying Script

  • Name Anti-flying script
  • Type Cheating prevention script for those who hate flyers.
  • Author Gamer3D
  • Description This script blasts flyers out of the sky. When it is placed in a brick to protect and it's sensitivity is adjusted, it will kill flyers, and leave plain loiterers alone.
  • This script is outdated, you can no longer use the flying bug.
  • Code
tracked = nil -- Keep track of checked players so we don't blow up plain loiterers
trackedPosition = nil

sensitivity = 1 -- 1 is normal. Higher values indicate higher sensitivity

function checkIfFlyer(player, brick, slot)
	local relativePos = brick.Position - player.Character.PrimaryPart.Position
	if (relativePos.magnitude < 4 * sensitivity) then -- Brick is within normal distance for flying
		if (relativePos.y > -2 * sensitivity) and (relativePos.y < sensitivity) then -- Brick is at a good height for flying
			if tracked = player then
				local moveDist = player.Character.PrimaryPart.Position - trackedPosition
				if (moveDist.magnitude * sensitivity > 8) then -- Player moved away, but kept brick. Almost certainly a flyer.
					return true
				end
			end
			tracked = player
			trackedPosition = player.PrimaryPart.Position
		end
	end
	return false
end

function makeExplosion(Pos) -- Blow up
	local myExplosion = Instance.new("Explosion")
	myExplosion.Position = Pos
	myExplosion.Parent = game.Workspace
end

-- Main loop
playerList = game.Players:children()
brickList = nil
while true do
	if (script.Parent.className == "Part") then
		for i = 1, #playerList do
			if checkIfFlyer(playerList[i], script.Parent, 1) then
				wait(0.5)
				makeExplosion(script.Parent.Position)
			end
		end
		playerList = game.Players:children() -- Keep track of new players too
	end

	if (#PlayerList == 0) then -- No sense bogging anything down.
		wait(10)
	end
	if (#playerList > 0) then
		wait(2)
	end
end

Talky Regeneration

  • Name TalkyRegenScript
  • Type Regens a model
  • Author matrix35
  • Description Regens a model by saying a word
  • Notes (in script)
  • Code


model = game.Workspace.Model -- The model to regenerate. Change Model to the models name
backup = model:clone() -- clones the model

function onChatted(msg, recipient, speaker) 

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

if (msg == "regen") then -- The word you want people to say to regen it. change it
model:remove() --removes the model
model = backup:clone() --clones the model
model.Parent = game.Workspace -- makes the parent the Workspac
model:makeJoints() -- generates the model
end 

end 

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

game.Players.ChildAdded:connect(onPlayerEntered) 

-- When you are done editing this, save this place then go to it then say the word to regenerate the model.

Turn all your bricks a certain color

  • Name Unknown
  • Type
  • Author Unknown
  • Description
  • Notes Put script in Workspace
  • Code
function turnBlack(where)
local ch=where:GetChildren() --note that we need local; see below
local i = 0
for i = 1, #ch do -- one number for each "child" 
if #ch[i]:GetChildren()>0 then turnBlack(ch[i]) end -- when to stop this loop 
if ch[i].className=="Part" or ch[i].className=="Seat" or ch[i].className=="SpawnLocation" then ch[i].BrickColor=BrickColor.new("Black") end -- what specific objects to change colors (e.g., bricks, not cameras)
end
end
turnBlack(game.Workspace)

What color is this brick?

  • Name What color is this brick?
  • Type
  • Author Unknown
  • Description
  • Notes Put script in brick
  • Code
function onTouched(hit) 

c=script.Parent.BrickColor
if c==BrickColor.new("Bright red") then
print("it's red!")
else
print("It's not red.")
end
end

script.Parent.Touched:connect(onTouched)

Random Messages

  • Note: This will select a random message from your prewritten group of messages and print it out.
local randommessages = {"Sample message 1",  "Sample message 2", "Sample message 3", "Sample message 4", "Sample message 5", "Sample message 6"}   
local rndmsg = math.random(1, 6) 
--Random number between 1, and the number of items in the table  
print(randommessages[rndmsg])  

Simple Breaking Ice

When the brick inside gets touched, it has a 3 in 5 chance for breaking for a moment.

  • Name Ice
  • Type Random number usage
  • Author WhoKnows OR main account TheName, WhoKnowsAndTheName on wiki.
  • Description
  • Notes Put script in brick
  • Code
function ont(hit)
hum = hit.Parent:findFirstChild("Humanoid")
if (hum ~= nil) then
	mn = math.random(1, 5)
	if (mn ~= 3) and (mn ~= 4) and (mn ~= 2) then
		print(mn)
		else
		print(mn)
		script.Parent.Transparency =   1
		script.Parent.CanCollide = false
		wait(0.5)
		script.Parent.Transparency = 0.5
		script.Parent.CanCollide = true
		end
	end
end
script.Parent.Touched:connect(ont)