Hello developers! I have made a kart tire script. However, I think it can use some improving.
What does it do:
It adds the kart settings that you have enabled via an inventory datastote. In my case, red tires, default body, and “Fasty” engine:
Script:
Here it is:
local debounce = false
local FLCframe = script.Parent.Wheels.FL.CFrame
local FRCframe = script.Parent.Wheels.FR.CFrame
local RLCframe = script.Parent.Wheels.RL.CFrame
local RRCframe = script.Parent.Wheels.RR.CFrame
function onChildAdded(hit)
if debounce == false then
local orentation = script.Parent.Wheels.RR.Parts.Rim.Orientation
if hit.Name == "SeatWeld" then
local human = hit.part1.Parent:FindFirstChild("Humanoid")
if (human ~= nil) then
local plr = game.Players:GetPlayerFromCharacter(human.Parent)
if plr then
local kartFolder = plr:FindFirstChild("Kart")
if kartFolder then
local wheels = kartFolder:WaitForChild("Tires")
local wheelsHolder = game.ReplicatedStorage.Tires
local selected = nil
for i, wheel in pairs(wheels:GetChildren()) do
if wheel:IsA("BoolValue") then
if wheel.Value == true then
selected = wheel.Name
end
end
end
if selected ~= nil then
local found = wheelsHolder:FindFirstChild(selected)
if found then
local fr = script.Parent.Wheels.FR
for i, v in pairs(fr.Parts:GetDescendants()) do
v:Destroy()
end
for i, ch in pairs(found:GetDescendants()) do
if ch:IsA("BasePart") then
local cl = ch:Clone()
cl.Parent = script.Parent.Wheels.FR.Parts
cl.Orientation = orentation
local weld = Instance.new("Weld", cl)
weld.Part0 = cl
weld.Part1 = script.Parent.Wheels.FR
--weld.C0 = cl.CFrame:Inverse()
--weld.C1 = script.Parent.Wheels.FR.CFrame:Inverse()
cl.CFrame = FRCframe
cl.Orientation = orentation - Vector3.new(0, 180, 0)
cl.Anchored = false
--cl.Parts:SetPrimaryPartCFrame(script.Parent.Wheels.FR.Parts.Rim.CFrame)
end
end
script.Parent.Wheels.FR.Anchored = false
--cl.Parts:SetPrimaryPartCFrame(script.Parent.Wheels.FR.Parts.Rim.CFrame)
end
--for _, v in pairs(fr.Parts:GetChildren()) do
-- v.Orientation = script.Parent.Wheels.FR.Parts.Rim.Orientation
--end
--local childFR = script.Parent.Wheels.FR:GetDescendants()
--childFR:Destroy()
--fr.Name = "FR"
local fl = script.Parent.Wheels.FR:Clone()
for i, v in pairs(script.Parent.Wheels.FL.Parts:GetDescendants()) do
v:Destroy()
end
for i, ch in pairs(found:GetDescendants()) do
if ch:IsA("BasePart") then
local cl = ch:Clone()
cl.Parent = script.Parent.Wheels.FL.Parts
cl.Orientation = orentation
local weld = Instance.new("Weld", cl)
weld.Part0 = cl
weld.Part1 = script.Parent.Wheels.FL
--weld.C0 = cl.CFrame:Inverse()
--weld.C1 = script.Parent.Wheels.FL.CFrame:Inverse()
cl.CFrame = FLCframe
cl.Orientation = orentation - Vector3.new(0, 180, 0)
cl.Anchored = false
--cl.Parts:SetPrimaryPartCFrame(script.Parent.Wheels.FR.Parts.Rim.CFrame)
end
end
script.Parent.Wheels.FL.Anchored = false
--for _, v in pairs(fl.Parts:GetChildren()) do
-- v.Orientation = script.Parent.Wheels.FL.Parts.Rim.Orientation
--end
local rl = script.Parent.Wheels.FR:Clone()
for i, v in pairs(script.Parent.Wheels.RL.Parts:GetDescendants()) do
v:Destroy()
end
for i, ch in pairs(found:GetDescendants()) do
if ch:IsA("BasePart") then
local cl = ch:Clone()
cl.Parent = script.Parent.Wheels.RL.Parts
cl.Orientation = orentation
local weld = Instance.new("Weld", cl)
weld.Part0 = cl
weld.Part1 = script.Parent.Wheels.RL
--weld.C0 = cl.CFrame:Inverse()
--weld.C1 = script.Parent.Wheels.RL.CFrame:Inverse()
cl.CFrame = RLCframe
cl.Orientation = orentation
cl.Anchored = false
--cl.Parts:SetPrimaryPartCFrame(script.Parent.Wheels.FR.Parts.Rim.CFrame)
end
end
script.Parent.Wheels.RL.Anchored = false
local rr = script.Parent.Wheels.FL:Clone()
for i, v in pairs(script.Parent.Wheels.RR.Parts:GetDescendants()) do
v:Destroy()
end
for i, ch in pairs(found:GetDescendants()) do
if ch:IsA("BasePart") then
local cl = ch:Clone()
cl.Parent = script.Parent.Wheels.RR.Parts
cl.CFrame = RRCframe
cl.Orientation = orentation
local weld = Instance.new("Weld", cl)
weld.Part0 = cl
weld.Part1 = script.Parent.Wheels.RR
-- weld.C0 = cl.CFrame * CFrame.Angles(math.rad(-90), 0, 0)
--weld.C1 = script.Parent.Wheels.RR.CFrame * CFrame.Angles(math.rad(-90), 0, 0) --script.Parent.Wheels.RR
cl.Anchored = false
cl.Orientation = orentation
--cl.Parts:SetPrimaryPartCFrame(script.Parent.Wheels.FR.Parts.Rim.CFrame)
end
end
script.Parent.Wheels.RR.Anchored = false
-- Refresh
wait()
--script.Parent.DriveSeat:WaitForChild("SeatWeld"):Destroy()
--print("Seat weld destroyed")
--wait(0.1)
--script.Parent.Start.Value = true
debounce = true
script.Parent.DriveSeat:WaitForChild("SeatWeld"):Destroy()
wait(3)
script.Parent.DriveSeat:Sit(human)
--script.Parent.DriveSeat:Sit(human)
end
end
end
end
end
end
end
script.Parent.DriveSeat.ChildAdded:Connect(onChildAdded)
Additional info
- The kart parts is stored in ReplicatedStorage
- The tires are parts
If you could optimize this script, that would be great!
Thank you, WE