I need help with fixing my code

This is a second part on one of my topics, ‘How do I weld boots to the players legs? (r6)’ My issue is that I’ve been working on a simple boot switching system for developers. The cloned boots only appear when the player’s character loads in again. Not even when the selected boots value is changed! This topic also fits in the Code Review category.

My entire code :

local DataStoreService = game:GetService('DataStoreService')
local DataBase = DataStoreService:GetDataStore("PlayerBase4")

local data = {}

game.Players.PlayerAdded:Connect(function(player)
	
	local success, playerData
	success, playerData = pcall(function()

		return DataBase:GetAsync(player.UserId)

	end)

	if success then

		if not playerData then

			playerData = {
				["SelectedBoots"] = "Default",
			}

		end

		data[player.UserId] = playerData

	end
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local height = Instance.new("NumberValue", leaderstats)
	height.Name = "Height"
	
	local selectedBoots = Instance.new("StringValue", player)
	selectedBoots.Name = "SelectedBoots"
	selectedBoots.Value = playerData.SelectedBoots
	
	player.CharacterAdded:Connect(function(character)
		
		local leftBoot = game:GetService('ReplicatedStorage'):WaitForChild("Boots"):FindFirstChild(selectedBoots.Value):Clone()
		leftBoot:SetPrimaryPartCFrame(character["Left Leg"].CFrame * CFrame.new(0,-.6,0) * CFrame.Angles(0,math.rad(270),0))
		leftBoot.Parent = character
		leftBoot.WeldConstraint.Part0 = leftBoot.PrimaryPart
		leftBoot.WeldConstraint.Part1 = character["Left Leg"]
		leftBoot.Name = "LeftBoot"

		local rightBoot = game:GetService('ReplicatedStorage'):WaitForChild("Boots"):FindFirstChild(selectedBoots.Value):Clone()
		rightBoot:SetPrimaryPartCFrame(character["Right Leg"].CFrame * CFrame.new(0,-.6,0) * CFrame.Angles(0,math.rad(270),0))
		rightBoot.Parent = character
		rightBoot.WeldConstraint.Part0 = rightBoot.PrimaryPart
		rightBoot.WeldConstraint.Part1 = character["Right Leg"]
		rightBoot.Name = "RightBoot"

		selectedBoots.Changed:Connect(function()
			
			data[player.UserId].SelectedBoots = selectedBoots.Value

			if character:FindFirstChild("LeftBoot") then

				character:FindFirstChild("LeftBoot"):Destroy()

			end
			if character:FindFirstChild("RightBoot") then

				character:FindFirstChild("RightBoot"):Destroy()

			end

			local leftBoot2 = game:GetService('ReplicatedStorage'):WaitForChild("Boots"):FindFirstChild(selectedBoots.Value):Clone()
			leftBoot2:SetPrimaryPartCFrame(character["Left Leg"].CFrame * CFrame.new(0,-.6,0) * CFrame.Angles(0,math.rad(270),0))
			leftBoot2.Parent = character
			leftBoot2.WeldConstraint.Part0 = leftBoot2.PrimaryPart
			leftBoot2.WeldConstraint.Part1 = character["Left Leg"]
			leftBoot2.Name = "LeftBoot"

			local rightBoot2 = game:GetService('ReplicatedStorage'):WaitForChild("Boots"):FindFirstChild(selectedBoots.Value):Clone()
			rightBoot2:SetPrimaryPartCFrame(character["Right Leg"].CFrame * CFrame.new(0,-.6,0) * CFrame.Angles(0,math.rad(270),0))
			rightBoot2.Parent = character
			rightBoot2.WeldConstraint.Part0 = rightBoot2.PrimaryPart
			rightBoot2.WeldConstraint.Part1 = character["Right Leg"]
			rightBoot2.Name = "RightBoot"

		end)
		
		while wait() do

			local humanoidPart = character:WaitForChild("HumanoidRootPart") or character:WaitForChild("Torso")

			if humanoidPart then

				height.Value = math.round(humanoidPart.Position.Y / 3)

			end

		end
		
	end)
	
end)

game.Players.PlayerRemoving:Connect(function(player)
		
	local success, playerData
	success, playerData = pcall(function()

		return DataBase:SetAsync(player.UserId, data[player.UserId])

	end)
	
end)
1 Like

whats the purpose of this part of the code?

while wait() do

			local humanoidPart = character:WaitForChild("HumanoidRootPart") or character:WaitForChild("Torso")

			if humanoidPart then

				height.Value = math.round(humanoidPart.Position.Y / 3)

			end

		end

why do you need to update the height if you are welding the boots on

If the boots are not on the bottom of the r6 leg you are applying it on, you need to set the offset of the connected boot to the leg.

The purpose of the height is like the height counter in Steep Steps, and the boots aren’t even in the character’s model as a child.

This is most likely the offending line. This will only run when a new character is loaded in. Because of all the setting-up prior to this line, by the time the game gets here, the character was already loaded in. Instead of referring to the character this way, try doing this:

local character = player.Character or player.CharacterAdded:Wait()

This way, we are creating a variable that actually gets the character instead of completely relying on a event that might’ve been ran before checking. instead of waiting for a character to load in right away, the game will try to get the player’s character just by referring to its path. You should then put the boot setup in a function, which should be called right after the character variable was set.

local character = player.Character or player.CharacterAdded:Wait()
function bootSetup()
--Setting up boot shinanigans
end
bootSetup()

Then, to top it all off, we need to run this function each time the player’s character is loaded back in!

local character = player.Character or player.CharacterAdded:Wait() --Getting character variable
function bootSetup()
--Setting up boot shinanigans
end
bootSetup()

player.CharacterAdded:Connect(function(c)
character = c --Re-assinging the character variable, since the old character was most likely destroyed.
bootSetup() --Setting up the boot again
end)

IT WORKED! THANK YOU!!!

Thanks to that other person.

I added some extra stuff, since the boot won’t be re-applied if the character dies.

Ok, I changed up my script. Thank you again!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.