Script problems

I’m confused why this is not working. I normally don’t have problems with this type of stuff but for some reason I am now.

So I have a script in ServerScriptService:

local Block = game.Workspace.Camera.Block
local Rain = game.Workspace.Camera.Rain
local CloudPiece1 = game.Workspace.Camera.Cloud.CloudPiece1
local CloudPiece2 = game.Workspace.Camera.Cloud.CloudPiece2
local CloudPiece3 = game.Workspace.Camera.Cloud.CloudPiece3

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message == "/e hello" then
			
			Block:Destroy()
			Rain:Destroy()
			CloudPiece1.BrickColor = BrickColor.new("Pearl")
			CloudPiece2.BrickColor = BrickColor.new("Pearl")
			CloudPiece3.BrickColor = BrickColor.new("Pearl")
		end
	end)
end)

But whenever I join it keeps giving me this error over and over again -
"Block is not a valid member of Camera “Workspace.Camera”

Also I should mention that the block part, rain part, and cloudpiece parts are being cloned from ReplicatedStorage into Camera. Thanks.

2 Likes

You need to do game.Workspace.Camera:WaitForChild(“Block”)

It probably doesn’t exist yet, so you need to wait for it.

5 Likes

All I can think of at this point is to add a wait

3 Likes

Thats what I tried earlier and it gave me an error -
"Infinite yield possible on ‘Workspace.Camera:WaitForChild(“Block”)’

2 Likes

Yea I tried that it also didn’t work.

2 Likes

That means that block never becomes a child of Camera or takes long enough the script gave up.

3 Likes

Maybe the block isn’t actually named “Block”. It could be named something else and you accidentally changed it while editing something.

3 Likes

Yea that’s what I thought but their are no mistakes in terms of spelling.

2 Likes

Have you looked in the explorer while testing? See that it actually ends up there?

Have you tried waiting like 10 seconds and then printing all children of camera?

The most likely issue is that it’s not detecting that as a child. Whether it be a spelling mistake or some issue with the logic that puts it in the camera.

3 Likes

Yea I have looked in explorer and it is their. It keeps giving me the error "Infinite yield possible on ‘Workspace.Camera:WaitForChild(“Block”)’

I saw that you said something about this before but is their anyway to fix this?

2 Likes

Ohh… Are you cloning from a local script? If you are then the server can’t see them. If you made it in a local script, only a local script can do anything with them (since it only exists for that player). So the server script you wrote won’t be able to access it.

2 Likes

The fix for that would be relatively simple

Just move the destruction and recolor logic to a function in a local script that activates when you fire a remoteEvent from the server.

3 Likes

Oh thanks, do you think you can show me how to do that exactly. I’m not very good at scripting and am just starting out. And too answer your question yes, it is cloning from a local script.

1 Like

Actually easiest solution since what you are checking for on the server is possible to do on the client is to just make the following a local script

local Block = game.Workspace.Camera.Block
local Rain = game.Workspace.Camera.Rain
local CloudPiece1 = game.Workspace.Camera.Cloud.CloudPiece1
local CloudPiece2 = game.Workspace.Camera.Cloud.CloudPiece2
local CloudPiece3 = game.Workspace.Camera.Cloud.CloudPiece3

game.Players.LocalPlayer.Chatted:Connect(function(message)
	if message == "/e hello" then
		
		Block:Destroy()
		Rain:Destroy()
		CloudPiece1.BrickColor = BrickColor.new("Pearl")
		CloudPiece2.BrickColor = BrickColor.new("Pearl")
		CloudPiece3.BrickColor = BrickColor.new("Pearl")
	end
end)
1 Like

I forgot the waitForChilds. I am lazy though and instead of typing that a lot I would just move all those variables into the if statement since by the time that code runs it should all be there anyways. (and if it errors, it won’t break the whole script)

It’s also worth noting that since you are using those variables only once, you might as well just use them in the line they are affecting. Usually when you set a variable like that it’s to simplify the path so you don’t have to type a long string over and over again. Or it’s to make something more readable. In my opinion this way is just as readable, but not everyone would agree.

game.Players.LocalPlayer.Chatted:Connect(function(message)
	if message == "/e hello" then
		game.Workspace.Camera.Block:Destroy()
		game.Workspace.Camera.Rain:Destroy()
		game.Workspace.Camera.Cloud.CloudPiece1.BrickColor = BrickColor.new("Pearl")
		game.Workspace.Camera.Cloud.CloudPiece2.BrickColor = BrickColor.new("Pearl")
		game.Workspace.Camera.Cloud.CloudPiece3.BrickColor = BrickColor.new("Pearl")
	end
end)

Actually a good example of what I think is a good use of that variable would be this

game.Players.LocalPlayer.Chatted:Connect(function(message)
	if message == "/e hello" then
        local camera = game.Workspace.Camera
        local cloud = camera.Cloud

		camera.Block:Destroy()
		camera.Rain:Destroy()
		cloud.CloudPiece1.BrickColor = BrickColor.new("Pearl")
		cloud.CloudPiece2.BrickColor = BrickColor.new("Pearl")
		cloud.CloudPiece3.BrickColor = BrickColor.new("Pearl")
	end
end)

But this kind of just falls down to preferences

2 Likes

Ok so if I understand I put one of those set of codes into a script in ServerScriptService. And then the cloning code which is:

local Block = game.ReplicatedStorage.Block:Clone()
local Rain = game.ReplicatedStorage.Rain:Clone()
local Cloud = game.ReplicatedStorage.Cloud:Clone()

Block.Parent = game.workspace.CurrentCamera
Rain.Parent = game.workspace.CurrentCamera
Cloud.Parent = game.workspace.CurrentCamera

I just put the cloning code in a local script in StarterPlayerScripts?

2 Likes

What I’ve said only applies if you have cloning done in a local script already. If it’s not already in a local script then this won’t work. The script I supplied should just be added either to the end of the cloning script, or as a separate local script.

3 Likes

Oh sorry I misunderstood. It works now, thank you so so much. I appreciate it your help and everyone elses help. :slight_smile:

3 Likes