Optimise my script on local parts

Hi! I’m quite new to scripting and I’ve made a script that is rather… horrible but it works, and I need help in making it more efficient. Please don’t bombard me with concepts that are rather ‘advanced’ like raycasting, etc.

The process goes like this:
When a player enters a building with a floor, it’ll remove the roof by parenting the roof to ReplicatedStorage. Once the player leaves the building, it’ll touch on a brick named Outside which will return back the roof. Mind you this will happen LOCALLY so other players don’t see the roof disappearing. (Imagine like a topdown game)

Even though it works, it’ll send out this when the roof is empty in the output:
Roof is not a valid member of Model “Workspace.Building” - Client - Indoor:11
as the roof is in ReplicatedStorage. I think this will affect the performance in the long run, so is there any easy advice around this?

local FOOT = script.Parent:WaitForChild("LeftFoot")
local RS = game:GetService("ReplicatedStorage")
local STRING = script:WaitForChild("LOCATION")

FOOT.Touched:Connect(function(HIT)
	STRING.Value = HIT.Name
	print(STRING.Value)
	
	if STRING.Value == "Floor" then
		local MODEL = HIT.Parent
		local ROOF = MODEL.Roof
		ROOF.Parent = RS
	else
		if STRING.Value == "Outside" then
			local MODEL = HIT.Parent
			print(MODEL.Name)
			local ROOF = MODEL:FindFirstChild("Roof")
			
			if ROOF then
				print("Present")
				
			else
				print("Roof")
				local ORIGINAL = RS:FindFirstChild("Roof")
				ORIGINAL.Parent = MODEL
			end
		end
	end
end)


	```

can i know what this is refering to?
by the way you dont need to change the parent rather change the transparency and cancollide property

edit1:
would this help?

solution
local player=game.Players.LocalPlayer
local chr=player.Character or player.CharacterAdded:Wait()
local l_foot=chr:WaitForChild("Left Foot")

local RS = game:GetService("ReplicatedStorage")
local floor="Floor"
local outside="Outside"

l_foot.Touched:Connect(function(hit_Part)
  if hit_Part.Name==floor then
    local model=hit_Part.Parent
    local roof=model:FindFirstChild("Roof")
    roof.Parent=RS
    print("roof is place in  "..RS.Name)
  elseif hit_Part.Name==outside then
    local model=hit_Part.Parent
    local roof=RS:FindFirstChild("Roof")
    if roof~= nil then
    print("roof in "..RS.Name)
      roof.Parent=model
    else
      print("roof already in "..model.Name)
      return
    end
  end
end)
summary what the above code actually does

1st get the character foot

then fire a event when the foot touches something

if touched==floor then place the roof inside replicated storage

if touched==outside then place the roof inside model

when foot touched==outside the code checks

if replicated storage contains the part

if yes place roof back inside model

if not return and do nothing

5 Likes

My bad! This script is in the character of the player. So, FOOT acts as the character touching parts that are relevant.

I changed the parent because I’m making a topdown game and when the roof is present, it ‘blocks’ the cursor so the gun is aimed towards the roof, and not to objects in the room.