What do you want to achieve? Keep it simple and clear!
I want to make it so scripts dont break when i set the Player.Character instead of using StarterCharacterScripts
What is the issue? Include screenshots / videos if possible!
When i make a rig morph and i want to test it using StarterPlayer, its scripts work fine and actually work but when i make a script so that when you do something, it morphs you into that specific rig but the scripts just.. stop working
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I’ve tried tutorials and searching on the forums, but no one seems to talk about or have a fix for it.
Do you guys have any idea on why this happens? i would be very thankful
The code isnt probably that good because i was just messing around trying to find out why
StarterCharacterScripts code:
game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function()
print("clicked (the second detector, not parented to rig and uses StarterCharacterScripts)")
end)
Code without StarterCharacterScripts:
local player = game.Players.LocalPlayer
player:GetMouse().Button1Down:Connect(function()
print(player.Name.." clicked (the first detector, parented to rig)")
end)
The morph code:
game.Players.PlayerAdded:Connect(function(p)
p.Chatted:Connect(function(msg:string)
if msg == "testmorph" then
local char = game.ReplicatedStorage.StarterCharacter:Clone()
char.Parent = workspace
char:PivotTo(p.Character:GetPivot())
p.Character:Destroy()
p.Character = char
local fix = script.CameraFix:Clone()
fix.Parent = char
fix.Enabled = true
print("morphed "..p.Name)
end
end)
end)
If you’re wondering what CameraFix is, its a script i made that fixes the camera because when i set a new character, the camera doesnt get set to it (if you have any other ways to fix it please let me know)
--ServerScript in ServerScriptService
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if msg == "testmorph" then
local oldChar = player.Character
local newChar = game.ReplicatedStorage.StarterCharacter:Clone()
newChar.Parent = workspace
newChar:PivotTo(oldChar:GetPivot())
-- Move all LocalScripts from old character to new one
for _, scriptObj in pairs(oldChar:GetChildren()) do
if scriptObj:IsA("LocalScript") then
scriptObj:Clone().Parent = newChar
end
end
player.Character = newChar
oldChar:Destroy()
-- Fix camera
local cam = workspace.CurrentCamera
cam.CameraSubject = newChar:WaitForChild("Humanoid")
cam.CameraType = Enum.CameraType.Custom
print("Morphed "..player.Name)
end
end)
end)
--LocalScript in StarterPlayerScripts or cloned into new character
local player = game.Players.LocalPlayer
player.CharacterAdded:Connect(function(char)
local humanoid = char:WaitForChild("Humanoid")
local cam = workspace.CurrentCamera
cam.CameraSubject = humanoid
cam.CameraType = Enum.CameraType.Custom
end)
--LocalScript in StarterPlayerScripts
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local function setupCharacter(character)
-- Clone reusable LocalScripts
for _, scriptObj in pairs(ReplicatedStorage.CharacterScripts:GetChildren()) do
if scriptObj:IsA("LocalScript") then
scriptObj:Clone().Parent = character
end
end
-- Clone tools (optional)
if ReplicatedStorage:FindFirstChild("CharacterTools") then
for _, tool in pairs(ReplicatedStorage.CharacterTools:GetChildren()) do
if tool:IsA("Tool") then
tool:Clone().Parent = character
end
end
end
-- Fix camera
local humanoid = character:WaitForChild("Humanoid")
local cam = workspace.CurrentCamera
cam.CameraSubject = humanoid
cam.CameraType = Enum.CameraType.Custom
end
player.CharacterAdded:Connect(setupCharacter)
ServerScript
--ServerScript in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local function morph(player, newRig)
local oldChar = player.Character
newRig.Parent = workspace
newRig:PivotTo(oldChar:GetPivot())
player.Character = newRig
oldChar:Destroy()
end
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if msg == "testmorph" then
local newChar = ReplicatedStorage.StarterCharacter:Clone()
morph(player, newChar)
print("Morphed "..player.Name)
end
end)
end)