로블록스 게임제작

물건 집어서 쓰레기통에 버리는 스크립트

Dean83 2024. 2. 13. 10:41

시나리오 : 

  • 집어올릴 물건 근처로 이동
    • 가까이 다가 가서 클릭 했을때만 손에 집어든다.
    • 워크스페이스 -> CanMove 폴더 안에 있는 오브젝트만 손에 들 수 있다
  • 한번 더 클릭하면 손에 든 물건을 떨어뜨린다
    • 특정 오브젝트 (예 : 쓰레기동) 근처에서 떨어트리면 쓰레기통에 버린다
      • 워크스페이스 -> Destination 폴더 안에 있는 오브젝트에만 버릴 수 있다.
    • 특정 오브젝트 근처가 아닌곳에서 떨어트리면 바닥에 떨어트리면서 해당 오브젝트를 삭제한다. 

 

local moveFolderName = "CanMove"
local destinationName = "Destination"
local dropYPos = 5

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = game.Players.LocalPlayer:GetMouse()
local object = nil
local movableObjects = workspace.CanMove:GetChildren()
local destinationObjects = workspace.Destination:GetChildren()

local mouse = player:GetMouse()
local target = nil


local function button1Up()
	
	local holdItem = IsHandItemHold()
	local destItem = GetNearleastDestinationItem()
	
	if holdItem ~= nil then
		RemoveItemFromHand(holdItem, destItem)
		return
	end
	
	if checkMovableItem(mouse.Target) == false then
		return
	end
	
	if player:DistanceFromCharacter(mouse.Target.Position) > 10 then
		return
	end

	target = mouse.Target:Clone()
	target.Size = mouse.Target.Size * 0.1
	target.CanCollide = false
	target.Anchored = false
	AddItemToAccesorry(target)
	
end

mouse.Button1Up:Connect(button1Up)

function GetNearleastDestinationItem()
	if destinationObjects == nil then
		return nil
	end
	
	for _, item in pairs(destinationObjects) do
		if player:DistanceFromCharacter(item.PrimaryPart.Position) < 10 then
			return item
		end
	end
	
	return nil
end

function IsHandItemHold()
	
	local attatchedItem = character:FindFirstChild(moveFolderName)
	if attatchedItem == nil then
		return nil
	end
	
	local items = attatchedItem:GetChildren()
	for _, item in pairs(items) do
		if checkMovableItem(item) then
			return item
		end
	end
	return nil
end

function checkMovableItem(hitItem)
	for _, item in pairs(movableObjects) do
		if item.Name == hitItem.Name then
			return true
		end
	end
	
	return false
end


function AddItemToAccesorry(item)
	
	local folder = character:FindFirstChild(moveFolderName)
	if folder == nil then
	 	folder = Instance.new("Folder")
		folder.Name = "CanMove"
		folder.Parent = character
	end
	
	local accessory = item
	accessory.Parent = folder

	local attachment = Instance.new("Attachment")
	attachment.Name = "AccessoryAttachment"
	attachment.Position = Vector3.new(2, 2, 2) -- 손 위치에 맞게 조정해주세요
	attachment.Parent = accessory

	local weld = Instance.new("Weld")
	weld.Name = "AccessoryWeld"
	weld.Part0 = accessory
	weld.Part1 = character.RightHand -- 캐릭터의 오른손에 부착합니다
	weld.C0 = CFrame.new(0, 0, 0) -- 액세서리의 초기 위치에 맞게 조정해주세요
	weld.Parent = accessory
	
end

function RemoveItemFromHand(holdItem, destItem)
	local items = holdItem:GetChildren()
	for _, item in pairs(items) do
		item:Destroy()
	end
	
	holdItem.Parent = workspace
	
	if destItem ~= nil then
		holdItem.Position = Vector3.new(destItem.PrimaryPart.Position.x,destItem.PrimaryPart.Position.y + dropYPos,destItem.PrimaryPart.Position.z)
		holdItem.CanCollide = true	
	end
	
	repeat wait () until holdItem.Position.y <= 0
	holdItem:Destroy()
end