Im trying to make a horse skin system for my game using WeldConstraints but right now it creates multiple weldConstraints in the same part, i tried to fix it but all it did was add One WeldConstraint in one Part and not the rest
heres the code:
local function equipSkin(Player, Horse, Skin)
if Skin == "Brown" then return end
local EquippedSkin = game.ReplicatedStorage:WaitForChild("HorseSkins"):WaitForChild(Horse.Name):FindFirstChild(Skin)
EquippedSkin.Parent = workspace
for i, V in pairs(Horse:GetChildren()) do
if V:IsA("Part") or V:IsA("UnionOperation") then
if V.Name ~= "Tail" then
V.Transparency = 1
end
for _, Parts in pairs(EquippedSkin:GetChildren()) do
if Parts:IsA("Part") or Parts:IsA("UnionOperation") then
Parts.Anchored = false
local WeldConstraint = Instance.new("WeldConstraint")
WeldConstraint.Name = "SkinWeld"
if V.Name == Parts.Name then
Parts.CFrame = V.CFrame
WeldConstraint.Part0 = Parts
WeldConstraint.Part1 = V
end
WeldConstraint.Parent = Parts
end
end
end
end
end
Horse is a Model, and I should probably clarify that “Skin” is a string. EquppedSkin is the model in a folder in replicated storage that matches the name of “Skin.”
Its because I want to try something different, I already made a horse system using a morph but i wanted to improve my scripting skills which is why I didn’t make it a morph
The problem is the fact that ur creating weldConstraints in ur script, so everytime the function is called, those WeldConstraints are created again.
try this:
this script checks if their is already weldConstraints with the name "SkinWeld" instead of checking if there are already on the parts.
local function equipSkin(Player, Horse, Skin)
if Skin == "Brown" then return end
local EquippedSkin = game.ReplicatedStorage:WaitForChild("HorseSkins"):WaitForChild(Horse.Name):FindFirstChild(Skin)
EquippedSkin.Parent = workspace
for i, V in pairs(Horse:GetChildren()) do
if V:IsA("Part") or V:IsA("UnionOperation") then
if V.Name ~= "Tail" then
V.Transparency = 1
end
for _, Parts in pairs(EquippedSkin:GetChildren()) do
if Parts:IsA("Part") or Parts:IsA("UnionOperation") then
Parts.Anchored = false
-- Check if a WeldConstraint already exists
local existingWeld = V:FindFirstChild("SkinWeld")
if not existingWeld then
local WeldConstraint = Instance.new("WeldConstraint")
WeldConstraint.Name = "SkinWeld"
WeldConstraint.Part0 = Parts
WeldConstraint.Part1 = V
WeldConstraint.Parent = V
end
end
end
end
end
end
local function equipSkin(Player, Horse, Skin)
if Skin == "Brown" then return end
local EquippedSkin = game.ReplicatedStorage:WaitForChild("HorseSkins"):WaitForChild(Horse.Name):FindFirstChild(Skin)
EquippedSkin.Parent = workspace
for _, horsePart in pairs(Horse:GetChildren()) do
if horsePart:IsA("Part") or horsePart:IsA("UnionOperation") then
if horsePart.Name ~= "Tail" then
horsePart.Transparency = 1
end
for _, skinPart in pairs(EquippedSkin:GetChildren()) do
if skinPart:IsA("Part") or skinPart:IsA("UnionOperation") then
skinPart.Anchored = false
if horsePart.Name == skinPart.Name then
local existingWeld = horsePart:FindFirstChild("SkinWeld")
if not existingWeld then
local WeldConstraint = Instance.new("WeldConstraint")
WeldConstraint.Name = "SkinWeld"
WeldConstraint.Part0 = skinPart
WeldConstraint.Part1 = horsePart
WeldConstraint.Parent = horsePart
end
skinPart.CFrame = horsePart.CFrame
end
end
end
end
end
end