There are some Problems in my Building System

Hey everyone!

I was working a building system for my new game Conquest!

Here are the issues:

  1. It can’t find the built/structure it is trying to make
    Output: unable to cast value to std::string

The issue can be found in:

local BuiltFolder = Builts:FindFirstChild(Type)  --- Error
local TeamFolder = BuiltFolder:FindFirstChild(Team) --- Error
local ClientBuilt = TeamFolder:FindFirstChild(structurebutton.Name):Clone() --- Error
  1. It can’t place the built/structure and the client structure only shows red even though it was within the max distance

The issue can be found in:

if hit and (HumanoidRootPart.Position - Base.Position).Magnitude < MaxDistance then
	GoodPlacement = true
	Built.BrickColor = BrickColor.new("Forest green")
else
	GoodPlacement = false
	Built.BrickColor = BrickColor.new("Really red")
end
  1. When I click on the cancel built button it shows some noticeable amount of lag when it’s clicked on too much

The issue can be found in:

BreakButton.MouseButton1Click:Connect(function() --- This causes a memory leak and lag issues
	PlacingBuilt = false
	ClientBuilt:Destroy()
	BreakButton.Visible = false
	TopHeader.Visible = true
end)

What had I tried so far:

I had tried to find/search some topics in devforum that had solve the “unable to cast value to std::string” error. Well, I did but not one of them give me enough information/clues to figure out the problem/error. Most of the topics I found was about Http request errors and that was not what I’m looking for.

Explorer:

image

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Builts = ReplicatedStorage:WaitForChild("Models")
local PlaceBuilt = ReplicatedStorage:WaitForChild(" y189d !T@L!d1 y;8dlool1; ld1 12l19 . ,. , .e.3;232..!Le3imjnDIlIi. ,2,ki#LKio32loJllil")

local UIS = game:GetService("UserInputService")
local RunS = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local StructureFrame = script.Parent
local char = Player.Character or Player.Character:Wait()
local Team = Player.TeamColor
local mouse = Player:GetMouse()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local BreakButton = StructureFrame.Parent.Parent.Parent.CancelButton
local TopHeader = StructureFrame.Parent.Parent.Parent.Frame
local YBuildingOffset = 0
local MaxDistance = 40
local RKeyPressed = false
local PlacingBuilt = false


for _, structurebutton in pairs(StructureFrame:GetChildren()) do
	if structurebutton:IsA("ImageButton") then
		local Type = structurebutton.Type.Value
		structurebutton.MouseButton1Up:Connect(function()
			BreakButton.Visible = true
			TopHeader.Visible = false
			StructureFrame.Parent.Parent.Visible = false

			local YRotation = 0
			local GoodPlacement = false
			local placedStructure

			if PlacingBuilt == false and Builts:FindFirstChild(Type) then
				PlacingBuilt = true

				local BuiltFolder = Builts:FindFirstChild(Type)  --- Error
				local TeamFolder = BuiltFolder:FindFirstChild(Team) --- Error
				local ClientBuilt = TeamFolder:FindFirstChild(structurebutton.Name):Clone() --- Error

				for i,Built in pairs(ClientBuilt:GetDescendants()) do
					if Built:IsA("Part") then
						Built.BrickColor = BrickColor.new("Forest green")
						Built.Material = "Neon"
						Built.Transparency = 0.4
						Built.CanCollide = false
						ClientBuilt.Parent = workspace

						local Base = ClientBuilt.PrimaryPart
						--print(Built)
						local StartingPosition = CFrame.new(0, -2, -15)
						Base.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingPosition)

						RunS.RenderStepped:Connect(function()
							local MousePos = mouse.UnitRay
							local CastRay = Ray.new(MousePos.Origin, MousePos.Direction*1000)
							local ignorelist = {ClientBuilt, char}
							local hit, position = workspace:FindPartOnRayWithIgnoreList(CastRay, ignorelist)

							if hit and (HumanoidRootPart.Position - Base.Position).Magnitude < MaxDistance then
								GoodPlacement = true
								Built.BrickColor = BrickColor.new("Forest green")
							else
								GoodPlacement = false
								Built.BrickColor = BrickColor.new("Really red")
							end
							local newRotationPos = CFrame.Angles(0, math.rad(YRotation), 0)
							local NewCFrame = CFrame.new(position.x, position.y + YBuildingOffset, position.z)
							Base.CFrame = NewCFrame*newRotationPos								
						end)
						UIS.InputBegan:Connect(function(input)
							if input.KeyCode == Enum.KeyCode.R then
								RKeyPressed = true

								local rotationSpeed = 5
								while RKeyPressed do
									wait()
									if PlacingBuilt == true then
										YRotation = YRotation + rotationSpeed									
									end
								end
							elseif input.UserInputType == Enum.UserInputType.MouseButton1 and PlacingBuilt == true and GoodPlacement == true then
								local StructureCFrame = Base.CFrame
								placedStructure = PlaceBuilt:InvokeServer(ClientBuilt.Name, StructureCFrame)
								if placedStructure == true then
									PlacingBuilt = false
									StructureFrame:Destroy()
									ClientBuilt:Destroy()
									TopHeader.Visible = true
								end
							end
						end)
						UIS.InputEnded:Connect(function(input)
							if input.KeyCode == Enum.KeyCode.R then
								RKeyPressed = false
							end
						end)
					end
				end
				BreakButton.MouseButton1Click:Connect(function() --- This causes a memory leak and lag issues
					PlacingBuilt = false
					ClientBuilt:Destroy()
					BreakButton.Visible = false
					TopHeader.Visible = true
				end)
			end
		end)
	end
end

The game Conquest! won’t be finished without this building system which is a major part of the game.
Any help is appreciated!

Solved Issues: 1 and 3

Mind sharing what line the error is on? Is is there no line that the error is showing.


regarding the color, not entirely sure.


Ok for the last issue, have you thought about looking into disconnecting your functions?
Here’s an article on that

You should probably disconnect the inputBegan and ended

1 Like

Thanks for the helpful article! The last issue has been solved.

I did some testing in studio and found out it the error is in line 37 which is

local TeamFolder = BuiltFolder:FindFirstChild(Team)

Ah that would make sense, you defined team as

local Team = Player.TeamColor

which returns a brickcolor.

FindFirstChild() requires a string so FindFirstChild(Player.TeamColor) errors


You can probably change it to

local Team = Player.Team.Name

It still didn’t work. I printed the variable Team and it outputs “Loading” which is one of the teams and it stills outputs the same error “unable to cast value to std::string” I wanted it to return a brickcolor so that when the player makes the building it will be on the player’s team color. If it didn’t return the correct brickcolor of the player’s teamcolor, the building would attack the player who originally built it. I hope you understand.

Did you make sure to use Team.Name within the FindFirstChild()
b/c even if you print player.Team it will return the object (will show up as the name of the team though)

Yes, I did but, it still outputs the same error. I think it was because it can’t use FindFirstChild three times in row but it can do it two times and I don’t know why. I’m not really sure.

1 Like