im trying to make a morph script system where the player can click any object, as long as is tag “morphable” , then the script make the player morph into the object. But it wont morph me. what when wrong?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local SmokeEmitter = require(script.Parent.SmokeEmitter)
local MorphRemoteEvent = ReplicatedStorage:WaitForChild("MorphRemoteEvent")
local MorphSystem = {}
function MorphSystem:addHumanoidRootPartToObject(object)
if object:FindFirstChild("HumanoidRootPart") then
return object:FindFirstChild("HumanoidRootPart")
end
local rootPart = Instance.new("Part")
rootPart.Name = "HumanoidRootPart"
rootPart.Size = Vector3.new(2, 2, 2)
rootPart.Anchored = true
rootPart.Transparency = 1
rootPart.CanCollide = false
rootPart.Parent = object
local weld = Instance.new("WeldConstraint")
weld.Part0 = rootPart
weld.Part1 = object:IsA("Model") and object.PrimaryPart or object
weld.Parent = rootPart
return rootPart
end
function MorphSystem:morphPlayerToObject(player, object)
if not CollectionService:HasTag(object, "Morphable") then
warn("The clicked object is not tagged as Morphable.")
return
end
local character = player.Character
if not character or not character:FindFirstChild("HumanoidRootPart") then
warn("Player's character or HumanoidRootPart not found.")
return
end
local rootPart = self:addHumanoidRootPartToObject(object)
if not rootPart then
warn("Failed to add or find a HumanoidRootPart for the object.")
return
end
local playerPosition = character.HumanoidRootPart.Position
SmokeEmitter:EmitSmoke(playerPosition, 50)
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.Transparency = 1
part.CanCollide = false
end
end
local morphClone = object:Clone()
morphClone.Parent = workspace
morphClone:SetPrimaryPartCFrame(CFrame.new(playerPosition))
for _, part in pairs(morphClone:GetDescendants()) do
if part:IsA("BasePart") then
part.Anchored = false
end
end
player.Character = morphClone
SmokeEmitter:EmitSmoke(morphClone.HumanoidRootPart.Position, 50)
end
-- Listen for RemoteEvent triggers from clients
MorphRemoteEvent.OnServerEvent:Connect(function(player, targetPath)
local target = workspace:FindFirstChild(targetPath)
if target then
print("Morph request from:", player.Name, "Resolved Target:", target.Name)
MorphSystem:morphPlayerToObject(player, target)
else
warn("Target not found in workspace:", targetPath)
end
end)
return MorphSystem