Podstawy programowania Lua w Roblox

Jak odnosić się do obiektów (Part) i zmienić ich kolor na losowy w Roblox Lua
W sekcji Workspace stwórz nowy Part i nazwij go MyNewPart

myPart = game.Workspace.MyNewPart
myPart.BrickColor = BrickColor.random()

Jak odnieść się do obiektu do którego dołączony jest dany skrypt i ustawić onTouch event w Roblox?

local item = script.Parent
local function OnTouch(otherPart)
	item.BrickColor = BrickColor.random()
end
item.Touched:Connect(OnTouch)

Losowa liczba w Roblox

print(math.random(1, 1000))

OnTouch event tylko dla humanoid w Roblox

local item = script.Parent
isReady = true

local function OnTouch(otherPart)
	local hum = otherPart.Parent:FindFirstChild("Humanoid")
	if isReady and hum then
		item.BrickColor = BrickColor.random()
		isReady = not isReady
		wait(1)
		isReady = not isReady
	end
end

item.Touched:Connect(OnTouch)

Jak pobrac gracza w Roblox? Wykrycie dotyku tylko dla gracza

local item = script.Parent
isReady = true

local function OnTouch(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if isReady and player then
		item.BrickColor = BrickColor.random()
		isReady = not isReady
		wait(1)
		isReady = not isReady
	end
end

item.Touched:Connect(OnTouch)

Jak klonować obiekty w Roblox?
Przykład klonowania obiektu po jego dotknięciu

local item = script.Parent
canBeCloned = true

local function CreateBall()
	if canBeCloned then
		local cloneItem = item:Clone()
		cloneItem.Parent = item.Parent
		cloneItem.Position += Vector3.new(8,0,0)
		canBeCloned = false
		wait(2)
		canBeCloned = true
	end
end

local function OnTouch(otherPart)
	CreateBall()
end

item.Touched:Connect(OnTouch)

Jak stworzyć planszę do gry w szachy w Roblox za pomocą kodu Lua

tileSize = 5
boardSize = 8
orderNumber = 0

for i=1,boardSize,1 do
	for z =1,boardSize,1 do
		local x = Instance.new("Part" ,workspace)
		x.Size = Vector3.new(tileSize,1,tileSize)
		x.Position = Vector3.new(i*tileSize,1,z*tileSize)
		if orderNumber%2==0 then
			x.BrickColor = BrickColor.Black()
		end
		orderNumber+=1
	end
	
	-- add one if boardSize is even
	if boardSize%2==0 then
		orderNumber+=1
	end
end

Stwórz obiekty z Server Storage (spawning). Obiekt (model) dodaj do Server Storage, a skrypt do ServerScriptService

local ss = game:GetService("ServerStorage")
local spawnModel = ss:WaitForChild("SpawnObject")
--local zombie = game.ServerStorage:WaitForChild("SpawnZombie")
while true do
	wait(6)
	local spawnItem = spawnModel:Clone()
	spawnItem.Parent = workspace
	wait(4)
	spawnItem:Destroy()
end

Wybuch bomby po dotknięciu w Roblox

local item = script.Parent

local function OnTouch(other)
	local hum = other.Parent:FindFirstChild("Humanoid")
	
	if hum then
		local expl = Instance.new("Explosion", item) --Create explosion and set it's parent
		expl.Position = item.Position
		expl.BlastRadius = 5 --0-100
		expl.BlastPressure = 10000
		expl.ExplosionType = Enum.ExplosionType.NoCraters
		
	end
end

item.Touched:Connect(OnTouch)

Proximity prompt in Roblox (Otwieranie i zamykanie drzwi)

local door = game.Workspace.Door
local item = script.Parent
local prompt = item.ProximityPrompt
local dist = 12

local function OnSwitch(other)
	door.Position += Vector3.new(0,dist,0)
	dist *= -1
end

prompt.Triggered:Connect(OnSwitch)

Teleportacja w Roblox

local thisTeleport = script.Parent.Base
local nextTeleport = script.Parent.Parent.T2.Base

local function MovePlayer(other)
	local humrp = other.Parent:FindFirstChild("HumanoidRootPart")
	local player = game.Players:GetPlayerFromCharacter(other.Parent)
	if humrp and player then
		humrp.Position = nextTeleport.Position + Vector3.new(0,5,0)
	end
end

thisTeleport.Touched:Connect(MovePlayer)

Blender to Roblox Addon: https://github.com/Roblox/roblox-blender-plugin

Tablice w Lua Roblox

local myArray = {123, "John", true}


for i=1, #myArray, 1 do
	print(myArray[i])
end


for i, item in pairs(myArray) do
	print(item)
end

Spawning group of monsters

local spawningGroup = game.ServerStorage.SpawningGroup:GetChildren()

for i,item in ipairs(spawningGroup)  do
	item:Clone()
	item.Parent = workspace
end

Spawn random monster

local quantity = 20
for i=1, quantity, 1 do
	wait(2)
	local rand = math.random(1, #enemyG)
	local enemy = enemyG[rand]:Clone()
	enemy.Parent = workspace
end

Spawning random monster in random place

local enemyGroup = game.ServerStorage.Enemies:GetChildren()
local spawnPoints = game.Workspace.SpawnPosition:GetChildren()

local quantity = 10

for i=1, quantity, 1 do
	wait(1)
	local enemyIndex = math.random(1, #enemyGroup)
	local enemy = enemyGroup[enemyIndex]:Clone()
	enemy.Parent = workspace
	local posIndex = math.random(1, #spawnPoints)
	enemy:MoveTo(spawnPoints[posIndex].Position + Vector3.new(0,5,0))
end

Create new part

local part = Instance.new("Part")
part.Shape = Enum.PartType.Cylinder
part.Position = Vector3.new(0,15,0)
part.Parent = workspace

local effect = Instance.new("Fire")
effect.Parent = part

Podstawowy pickup prędkości

local shoe = script.Parent

local function OnTouch(other)
	local hum = other.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(other.Parent)
	if hum and player then
		hum.WalkSpeed += 20
		shoe:Destroy()
		wait(4)
		hum.WalkSpeed -= 20
		
	end
end

shoe.Touched:Connect(OnTouch)

gun

-- script
local remoteEvent = script.Parent.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player, target)
	target.Humanoid.Health -= 20
end)

--local script
local remoteEvent = script.Parent:WaitForChild("RemoteEvent")

local player = game.Players.LocalPlayer
local gun = script.Parent

local mouse = player:GetMouse()
local coolDown = true
local muzzle = gun.Gun.MuzzleAtt.Muzzle

gun.Activated:Connect(function()
	if coolDown then
		muzzle:Emit(1) -- set particles Rate to 0 in Properties 
		coolDown = false
		script.Parent.Shot:Play()
		local target = mouse.Target
		local hum = target.Parent:FindFirstChild("Humanoid")
		if hum then
			remoteEvent:FireServer(target.Parent)
		end
		wait(0.5)
		coolDown = true
	end
	
end)

Serce uzupełniające życie

local heart = script.Parent

local function OnTouch(other)
	local hum = other.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(other.Parent)
	if hum and player then
		hum.Health = hum.MaxHealth
		heart:Destroy()
	end
end

heart.Touched:Connect(OnTouch)

Potion pickable tool (add RemoteEvent)

local potion = script.Parent

potion.Equipped:Connect(function()
	potion.Handle.Size *= 0.5
end)

potion.Unequipped:Connect(function()
	potion.Handle.Size *= 2
end)

potion.Activated:Connect(function()
	local char = potion.Parent
	local hum = char.Humanoid
	if hum then
		hum.Health = hum.MaxHealth
		potion:Destroy()
	end
end)

Pobieranie danych z klawiatury lub myszy

--local script in StarterPlayerScript
local UserInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer

UserInputService.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.X then
		print("key ", input.KeyCode, " was pressed")
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been pressed!")
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonR1 then
			print("gamepad used")
		end
	end
end)

Pobieranie Character, Humanoid, HumanoidRootPart z localscript

--local script
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Humanoid =  character:WaitForChild("Humanoid")

Pobieranie character, player z script

local coin = script.Parent

local function OnTouch(other)
	local player = game.Players:GetPlayerFromCharacter(other.Parent)
	local hum = other.Parent.Humanoid
	local humrp = other.Parent.HumanoidRootPart
	if player then
		print("touched")
		hum.Health -= 2
		humrp.Position += Vector3.new(10,10,10)
	end
end

coin.Touched:Connect(OnTouch)

Roblox backpack przechowuje Tools

-- Accessing Backpack from a Server Script:
game.Players.PlayerName.Backpack

-- Accessing Backpack from a LocalScript:
game.Players.LocalPlayer.Backpack
Scroll to Top