Cookbook (Chapter 6)

From Legacy Roblox Wiki
Jump to navigationJump to search

Introduction

Thus far we've interacted primarily with standard objects (e.g. Part, HopperBin, TextButton). In Recipe 4.2 we dabbled with special objects called structural objects (recall these are non-creatable objects used to categorize standard objects). There is another type of object called a service level object. Service level objects are very important objects that control a lot of functionality (typically an entire sector of functionality in ROBLOX). For example, we've been using the keyword “game” quite a bit. What is “game” though? Its simply a service level object, its correct name is DataModel. Its the most important service level object because it houses all other services (with a few exceptions).

The Workspace is also a service level object. It controls all rendered objects in ROBLOX. In this chapter you'll explore many of the commonly used services.

Using GetService

Problem

You want to get a service.

Solution

Use the GetService method.

print(game:GetService('Workspace')) --> Workspace

Discussion

In Recipe 6.0 I mentioned that most services are children of the DataModel. Why do we use GetService? This is because if a service doesn't exist, GetService automatically creates it for you. You don't have to stop using Workspace, because the parent of Workspace is locked, however for services like Lighting whose Parent property is not locked, it may be beneficial to use.

Getting Server Time

Problem

You want to figure out how long the server has been running.

Solution

Use the DistributedGameTime property of Workspace.

print(Workspace.DistributedGameTime)

Discussion

The DistributedGameTime property is how long the server has been up in seconds.

Using Lighting

Problem

You want to manipulate the Lighting of your place.

Solution

Use the Ambient, Brightness, and TimeOfDay properties.

game.Lighting.Ambient = Color3.new(1, 0, 0)
game.Lighting.Brightness = .5
game.Lighting.TimeOfDay = '01:00:00'

Discussion

The Ambient property will change the hue of the ambient lighting in your place, in this case we turnt it to a red color. The Brightness property sets the brightness of the ambient light. The default is 1, so I set it to be a tad lower. The last property TimeOfDay sets the time of day in which the sky will simulate, in this case we're simulating 1 AM in the morning so it will be a dark sky.

Awarding Badges

Problem

You want to award a badge.

Solution

Use the AwardBadge method.

game:GetService('BadgeService'):AwardBadge(game.Players.Player.userId, 1234)

Discussion

We first proceed to get the BadgeService. The AwardBadge method is then used, it accepts the ID of the user you want to award the badge to, then the ID of the badge itself. To get the in game ID of a player, you use the userId property of their Player.

Creator Badge

Problem

You want to award people for meeting the creator of the game.

Solution

Use the AwardBadge method and the CreatorId property.

local creatorHere = function() 
	for _,v in ipairs(game.Players:GetPlayers()) do 
		if v.userId == game.CreatorId then 
			return true
		end
	end 
end 
 
game.Players.PlayerAdded:connect(function(pl)
        if pl.userId == game.CreatorId then
		for _,v in ipairs(game.Players:GetPlayers()) do 
			game:GetService('BadgeService'):AwardBadge(v.userId, 1234)
		end                 
	elseif creatorHere() then 
		game:GetService('BadgeService'):AwardBadge(pl.userId, 1234)
	end 
end)

Discussion

The “creatorHere” function will return true if one of the players in game is the creator. We then proceed to connect the PlayerAdded event and check if the newly added player is the creator. If so then we want to award the badge to any person already in the game. If the newly added player is not the creator of the game, then we check if the creator is there. If so, then they are awarded the badge.

Inserting Assets

Problem

You want to insert a free model from a script, into your game.

Solution

Use the LoadAsset method.

game:GetService('InsertService'):LoadAsset(1234):GetChildren()[1].Parent = Workspace

Discussion

The LoadAsset method takes an argument of what the ID of the asset you're trying to get. Make sure that the asset is in your models or else you won't be able to use it. Then it returns a model with the inserted asset in it. To get the first member of this wrapping model I use GetChildren and index the first one. Then this gets parented to the Workspace.

Retrieving IPs

Due to a recent update, you can no longer do this!

Problem

You want to get the IP address of an in game player.

Solution

Use the name of children of the NetworkServer.

local player, ii = game.Players.Player
for i,v in ipairs(game.Players:GetPlayers()) do
        if v == player then
                ii = i
                break
        end
end
 
print(game:GetService('NetworkServer'):GetChildren()[ii].Name)

Discussion

There are objects (without a className) inside of a service called NetworkServer. These names of these objects are the IP addresses of the Players. They are in order from first joined, to last. We just had to get the index in the Players directory and use that index to get the correct child of NetworkServer and print the name.

Using Debris

Problem

You want to remove an object after a certain amount of time (without interrupting the script's execution).

Solution

Use the AddItem method of Debris.

game:GetService('Debris'):AddItem(Workspace.Part, 5)

Discussion

The Debris service is used to get objects to automatically clean themselves up after a certain amount of time. You can add an item to the items to be cleaned list by using AddItem. Then as a second argument you pass it the amount of time before the object is cleaned in seconds.

Continue on to Chapter 7, Moving