Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why this package giver script works in studio but not in game? [function]

So basically this script is in a part, end when the player touches the character, it gives him a package.

It does work in Studio, but not in game.

Here it is :

local giver = script.Parent
local preview = giver:WaitForChild("Preview")
local packageId = giver:WaitForChild("PackageId")
local currentPackageId = 0

local market = game:GetService("MarketplaceService")
local insert = game:GetService("InsertService")
local assets = game:GetService("AssetService")
local debris = game:GetService("Debris")

local function updatePackage(newId)
	if newId == currentPackageId then return end
	
	local info = market:GetProductInfo(newId)
	local assetType = info.AssetTypeId
	if assetType == Enum.AssetType.Package.Value then
		currentPackageId = newId
		preview.Texture = "https://www.roblox.com/asset-thumbnail/image?width=420&height=420&format=png&assetId=" .. newId
	else
		warn(newId,"is not a package!")
		packageId.Value = currentPackageId
	end
end

updatePackage(packageId.Value)
packageId.Changed:connect(updatePackage)

local R15_StackPriority =
{
	HumanoidRootPart = 0;
	LowerTorso = 1;
	UpperTorso = 2;
	Head = 3;
	
	LeftUpperArm = 3;
	LeftLowerArm = 4;
	LeftHand = 5;
	
	RightUpperArm = 3;
	RightLowerArm = 4;
	RightHand = 5;
	
	LeftUpperLeg = 2;
	LeftLowerLeg = 3;
	LeftFoot = 4;
	
	RightUpperLeg = 2;
	RightLowerLeg = 3;
	RightFoot = 4;
}

local function getTool(char)
	for _,v in pairs(char:GetChildren()) do
		if v:IsA("Tool") then
			return v
		end
	end
end

local function prioritySort(a,b)
	return R15_StackPriority[a.Parent.Name] < R15_StackPriority[b.Parent.Name]
end

local function unload(bin,parent)
	for _,v in pairs(bin:GetChildren()) do
		v.Parent = parent
	end
	bin:Destroy()
end

local function bumpCharacterScales(humanoid)
	for _,v in pairs(humanoid:GetChildren()) do
		if v:IsA("NumberValue") then
			local value = v.Value
			v.Value = value - 0.01
			v.Value = value
		end
	end
end

local function getCurrentPackage(packageType)
	local id = packageId.Value
	local package = Instance.new("Folder")

	local r6 = Instance.new("Folder",package)
	r6.Name = "R6"
	
	local r15 = Instance.new("Folder",package)
	r15.Name = "R15"
		
	local packageIds = assets:GetAssetIdsForPackage(id)
	for _,packageId in pairs(packageIds) do
		local limb = insert:LoadAsset(packageId)
		if limb:FindFirstChild("R6") then
			unload(limb.R6,r6)
			unload(limb.R15,r15)
		else
			unload(limb,r6)
		end
		limb:Destroy()
	end
	
	return package
end

local function applyPackage(character)
	local charPackageId = character:FindFirstChild("PackageId")
	if not charPackageId then
		charPackageId = Instance.new("IntValue")
		charPackageId.Name = "PackageId"
		charPackageId.Parent = character
	end
	
	if charPackageId.Value ~= packageId.Value then
		charPackageId.Value = packageId.Value
		
		local package = getCurrentPackage()
		
		local humanoid = character:WaitForChild("Humanoid")
		local rigType = humanoid.RigType.Name
		
		if rigType == "R6" then
			for _,v in pairs(character:GetChildren()) do
				if v:IsA("CharacterMesh") then
					v:Destroy()
				end
			end
			for _,v in pairs(package.R6:GetChildren()) do
				v:Clone().Parent = character
			end
		else 
			local rigAttachmentMap = {}
			
			local newPackage = Instance.new("Folder")
			for _,v in pairs(package.R15:GetChildren()) do
				v:Clone().Parent = newPackage
			end
			
			if #newPackage:GetChildren() > 0 then
				local accessories = {}
				
				for _,v in pairs(character:GetChildren()) do
					if v:IsA("Accoutrement") then
						table.insert(accessories,v)
						v.Parent = nil
					end
				end
			
				local head = character:WaitForChild("Head")
				local neck = head:WaitForChild("NeckRigAttachment")
				local rootPart = character:WaitForChild("HumanoidRootPart")
				local root = rootPart:WaitForChild("RootRigAttachment")
		
				for _,limb in pairs(newPackage:GetChildren()) do
					for _,v in pairs(limb:GetChildren()) do
						if v:IsA("Attachment") and v.Name:find("RigAttachment") then
							local jointName = v.Name:gsub("RigAttachment","")
							if not rigAttachmentMap[jointName] then
								rigAttachmentMap[jointName] = {}
							end
							table.insert(rigAttachmentMap[jointName],v)
						end
					end
				end
		
				table.insert(rigAttachmentMap.Neck,neck)
				table.insert(rigAttachmentMap.Root,root)
				
				local mappedLimbs = {}
				
				for jointName,joints in pairs(rigAttachmentMap) do
					if #joints == 2 then
						table.sort(joints,prioritySort)
						
						local m = Instance.new("Motor6D")
						m.Name = jointName
						m.C0 = joints[1].CFrame
						m.C1 = joints[2].CFrame
						m.Part0 = joints[1].Parent
						m.Part1 = joints[2].Parent
						m.Parent = m.Part1
						m.Part0.Parent = character
						m.Part1.Parent = character
						mappedLimbs[m.Part0] = true
						mappedLimbs[m.Part1] = true
					end
				end
				
				local tool = getTool(character)
				if tool then
					humanoid:UnequipTools()
				end
				
				for _,v in pairs(character:GetChildren()) do
					if v:IsA("BasePart") and R15_StackPriority[v.Name] and not mappedLimbs[v] then
						v.Name = ""
						v:Destroy()
					end
				end
				
				for _,accessory in pairs(accessories) do
					accessory.Parent = character
				end
				
				if tool then
					humanoid:EquipTool(tool)
				end
				
				bumpCharacterScales(humanoid)
			end
		end
		package:Destroy()
	end
end

local function onTouched(hit)
	local char = hit.Parent
	if char then
		local humanoid = char:FindFirstChild("Humanoid")
		if humanoid then
			applyPackage(char)
		end
	end
end

giver.Touched:connect(onTouched)

2 comments

0

Do you have FilteringEnabled on?

0

nop

1 answer

The reason it doesn't work is because in the roblox studio since you are working from your pc the client and the server are the same thing but in game they aren't ,hopefully you can figure the rest out

This discussion is preserved as it was. New answers, comments, and votes are closed.