top of page
Search
  • Writer's pictureSaman khorram

frequently used

Updated: Aug 17, 2023


 

Find Player


local functionCallback(otherPart)
	local hum = otherPart.Parent:FindFirstChild("Humanoid")
	if hum then
		local player = game.Players:GetPlayerFromCharacter(hum.Parent)
	end
end
somePart.Touched:Connect(FunctionCallback)

- - - - - -

in ProximityPrompt we can connect to Trigger a function callback with an output parameter


local function OnRelease(Player)
	if Player then
		print(Player.Name)
	end
end
proxPromp.Triggered:Connect(OnRelease)

 

Random Time

initializing:


local timeoff = Random.new():NextNumber(0.8, 2)
local timeon = Random.new():NextNumber(1,1.2)
using directly in wait:
task.wait(Random.new():NextNumber(1 , 4) )--> returns with decimal 
task.wait(math.random(min, max) ) --> returns integer value

 

Colors. Materials


lightpart.BrickColor = BrickColor.new("Really red")
lightpart.Color = Color3.new(0, 0, 0)
lightpart.Material = Enum.Material.Plastic

 

Accessing Enum variables

explode.ExplosionType = Enum.ExplosionType.Craters

lightpart.Material = Enum.Material.Plastic


 

sweep out memory

local debrisSrv = game:GetService("Debris")

- - - In an event call back call AddItem to add a model to debris service for clean up after a manner of time


debrisSrv:AddItem(landmineModel, timeToDestroy)


 

Instantiate

Right after instantiate specify the parent and position


local explode = Instance.new("Explosion", landminebody)
explode.Position = landminebody.Position


 

Switch to make things happen just one time


 

Storing items in the server and spawning from there, note we cant place script connected to this process in the server storage, should stay in ServerScriptService

local ServerStorageSRVC = game:GetService("ServerStorage")
local ZombiesFolder = ServerStorageSRVC:WaitForChild("Zombies")
local DroolingZombies = ZombiesFolder:WaitForChild("DroolingZombies")

local DebrisSRVC = game:GetService("Debris")
local spawnRate = script:WaitForChild("SpawnRate")
local roundTime = script:WaitForChild("RoundTime")
local Activated = script:WaitForChild("Activated")


function spawnZambiesGrpA()	
	local dupZombies = DroolingZombies:Clone()
	dupZombies.Parent = workspace
	DebrisSRVC:AddItem(dupZombies, roundTime.Value)
	---can use this way to destroy aswell
	--task.wait(10)
	--dupZombies:Destroy()
end


if spawnRate and roundTime and Activated then
	while true do
		task.wait(spawnRate.Value)
		if Activated.Value then
			spawnZambiesGrpA()
		end		
	end			
end

 

to clone a model in a raw column pattern

note model doesnt have a position to manipulate we will use GetPivot()

for i=1, 10 do
	for j=1,10 do
		local dupmodel = model:Clone()
		dupmodel.Parent = workspace
		dupmodel:MoveTo(Vector3.new(
			model:GetPivot().X + distance*i * randx,
			model.landmine_base.Position.Y,
			model:GetPivot().Z + distance*j * randx))
	end
end


 

Characters part that holds the position and orientation of the character

, in case of teleporting so we can access this part, it is common between all characters,


local humanoidRoot = otherpart.Parent:FindFirstChild("HumanoidRootPart")

to know if it is a player then we need to filter it, we need a player from player service


local playersSrvc = game:GetService("Players")
to check if a part belongs to a player :
local player = playersSrvc:GetPlayerFromCharacter(otherpart.Parent)

how to give the character a new position,

we get CFrame of character and give it a new CFrame (a vector3), by default facing down the Z axis)

Sample script for teleporting

local pad = script.Parent.Pad
local teleport = script.Parent.teleport
local PlayerSrvc = game:GetService("Players")
local bIsDone = false --just a safe way to not let many parts of a character get evaluated at a task time--

local function doTeleport(otherpart)	
	local humanoidRoot = otherpart.Parent:FindFirstChild("HumanoidRootPart")
	local character = otherpart.Parent
	if	humanoidRoot then
		local player = PlayerSrvc:GetPlayerFromCharacter(character)
		if player and not bIsDone then
			bIsDone = true
			print(player.Name)
			humanoidRoot.CFrame = CFrame.new(teleport.Position) + Vector3.new(0,3,0)
			--humanoidRoot.Position = teleport.Position + Vector3.new(0,3,0)
			task.wait(1)
			bIsDone = false
		end
	end	
end

pad.Touched:Connect(doTeleport)

 

wait for 80% of a sound find duration then do something else


task.wait(soundNode.TimeLength * .8)

 

changing Attributes or TRS over time,


for i = 1 , 80, 1 do
	Part.Size  = part.Size + Vector3.new(0.3,5,5)
	task.wait(0.1)
end

5 views0 comments
bottom of page