What's wrong with this script?

So, I’m having trouble with a script. Not sure what’s causing it.

image

The image is referring to this line:

local housemodel = replicatedstorage.HouseModels:FindFirstChild(item).House:Clone()

Simply said, the RemoteEvent includes the name of the item that’s supposed to replace the previous house, deletes the old and spawns in a new one.
Code:

replicatedstorage = game:GetService("ReplicatedStorage")
local catalogstuff = replicatedstorage.Houses
local assets = replicatedstorage.HouseModels

local x 
local y 
local z 

local userplot 

script.Parent.OnServerEvent:connect(function(player,item)
	--find the currently-equipped item
	for i,v in pairs(player.EquippedHouse:GetChildren()) do
		v:Destroy()	
	end
	--replace the item
	for i,v in pairs(catalogstuff:GetChildren()) do
		if v.Name == item then
			local item = v:Clone()
			item.Parent = player.EquippedHouse
			--remove existing house
			for i,v in pairs (game.Workspace.HousePlot:GetChildren()) do
				if v.Owner.Value == player.Name then
					v.Plot.House:Destroy()
					--place the new one
					local housemodel = replicatedstorage.HouseModels:FindFirstChild(item).House:Clone()
					housemodel.Parent = v.Plot
					--move house
					housemodel:SetPrimaryPartCFrame(v.Plot.CFrame)
					print("Moved house")
					--get land orientation
					x = v.Plot.Orientation.X
					y = v.Plot.Orientation.Y
					z = v.Plot.Orientation.Z
					--rotate house
					housemodel.PrimaryPart.Orientation = Vector3.new(x, y, z)
					print("Rotated house to match the land")
				end
			end
		end		
	end
end)

Best regards.

Try this:

local housemodel = replicatedstorage.HouseModels:FindFirstChild(item.Name).House:Clone()

Also make sure to check whether housemodel is nil or not before doing anything with it because there is a chance FindFirstChild doesn’t find the object you were looking for.

1 Like

I am also aware of that solution, but it doesn’t work either.

The item bit, through the event, is classified as Button.name, hence why it can’t use that. But even if I were to remove the .name bit from the script that fires the RemoteEvent, the issue still persists.

What do you get in the output if you add print(item)?

1 Like

Nothing. The item is a ‘TextButton’ instance. Not the name of the button (like Button.name).

Are you getting any other errors?

1 Like

Other than that, no. I’ve been trying to resolve this for the past hour.

Right after this line, item doesn’t seem to be a string anymore.

I actually didn’t realise I changed the item value throughout the script. It works now.
Thanks!

I’m glad I was able to help you! :+1:

1 Like