로블록스 개발 4단계

Oak_Cassia·2022년 5월 18일
0

로블록스 개발기

목록 보기
4/5

왠지 모르겠지만 로컬 파일로 저장한 것에서 계속 오류가 난다.
그런데 클라우드에서 받아온 것은 잘된다. 이것 때문에 시간을 많이 버렸다...

다음 사진을 보자

시작 화면을 만들었고

  • 처음부터 진행하는 새 게임 시작
  • 이어서 할 수 있는 이어하기
    를 만들었다.

그리고 우측상단의 Level을 만들어 진행상황을 저장하게 했다!

C++ 이나 C#처럼 하나의 함수를 만들고 이를 호출하고 싶었다.
구글링으로 스택 오버플로에서 shared를 발견했다.

아래와 같이 shared를 사용해 Level에 따른 스폰위치 변경 스크립트를 작성했다.

  • 위치는 Workspace 하위
shared["Respawn"]= function(player) 
	local Leaderstat =player:WaitForChild("leaderstats")
	local _Level = Leaderstat["Level"]
	local level= _Level.Value
	if(level==1) then
		player.Character:SetPrimaryPartCFrame(game.Workspace.CheckPoint0.CFrame)
	end
	if(level == 2) then
		player.Character:SetPrimaryPartCFrame(game.Workspace.CheckPoint1.CFrame)
	end
	if(level == 3) then
		player.Character:SetPrimaryPartCFrame(game.Workspace.CheckPoint2.CFrame)
	end
	if(level == 4) then
		player.Character:SetPrimaryPartCFrame(game.Workspace.CheckPoint3.CFrame)
	end
	if(level == 5) then
		player.Character:SetPrimaryPartCFrame(game.Workspace.CheckPoint4.CFrame)
	end
end 

리스폰 방식을 바꾸면서 체크 포인트(스폰 위치) 스크립트도 간단하게 변경했다.

local spawn = script.Parent
	
function Ontouched(part)
	if part and part.Parent and part.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)

		if player.leaderstats.Level.Value < 2 then
			player.leaderstats.Level.Value =2
		end
	end
end
spawn.Touched:connect(Ontouched)
	

이제 새로운 체크 포인트를 만들면 Level의 값을 변경 해준 뒤
Respawn 함수에 추가하면 된다.
(CheckPoint3 의 위치에 파트 이름)

player.Character:SetPrimaryPartCFrame(game.Workspace.CheckPoint3.CFrame)

탐색기 계층


start 버튼을 누르면

script.Parent.MouseButton1Click:connect(function()
	local player= script:FindFirstAncestorOfClass("Player")
	script.Parent.Parent.Parent:Destroy() // gui 없애는 과정
	player:LoadCharacter()
	repeat wait() until shared["Respawn"]
	shared.Respawn(player) // 이 부분으로 호출
	
end)

자동 스폰(Load)를 멈추고 gui 띄우기

ServerScriptService에 넣어서 바로 수행된다.
아래 나오는 데이터 저장도 마찬가지

  • StopSpawn_ShowUi(script)
game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(player)
	local mainUi= game.ServerStorage:WaitForChild("WhenStart")
	local mainUiClone=mainUi:Clone()
	mainUiClone.Parent= player.PlayerGui

	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Died:Connect(function()
				wait(5)
				player:LoadCharacter()
				repeat wait() until shared["Respawn"]
				shared.Respawn(player)
				
			end)
		end
	end)
end)

data를 저장하고 불러오는 기능

-ServerScriptService 하위의 Script


local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local level = Instance.new("IntValue")
	level.Parent = leaderstats
	level.Name = "Level"
	level.Value = 1

	local data = dataStore:GetAsync(player.UserId)
	if data then
		level.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(Load)
	dataStore:SetAsync(Load.UserId, Load.leaderstats.Level.Value)
end)

이틀 간 고생했고이제 컨텐츠를 늘리는 일만 남았다

profile
rust로 뭐할까

0개의 댓글