Script only works in studio but not in-game?

Hello Devs,

I’ve created a script that, when you press a button you become a sort of Part/Model. Whenever the button is clicked, your character goes invisible and is unable to move. The part/model is cloned into the workspace in the position of the player. The camera then switches to the part. (Keep in mind this is supposed to be player specific, which means it’s only supposed to happen to one player)

The script works In studio. But whenever I test the script in-game, the script doesn’t work at all.
I have two scripts, one inside serverscriptservice, and the other inside starterGUI.

Here is my current code:
(Local script inside starterGUI)

local Camera = workspace.CurrentCamera
local remote = game.ReplicatedStorage:WaitForChild("Remote")
local value = game.workspace.Values.Walllnut

function onClicked(Player)
	print("ButtonHasBeenPressed")
	remote:FireServer("ToasterIsFired")
	wait(0.1)
	local number = value.Value
	local part = workspace:WaitForChild(number)
	Camera.CameraSubject = part:WaitForChild("Handle")
end
game.workspace.StartButton.ClickDetector.MouseClick:connect(onClicked)

(Script inside serverscriptservice)

local object = game.workspace.Wallnut
local remote = game.ReplicatedStorage:WaitForChild("Remote")
local value = game.workspace.Values.Walllnut

remote.OnServerEvent:Connect(function(player, request)
	if request == "ToasterIsFired" then
		local number = math.random(1,999999)
		value.Value = number
		print("Request Received")
		print(player.Character.Head.Position)
		local ClonedObject = object:Clone()
		ClonedObject.Parent = workspace
		ClonedObject.Name = (number)
		ClonedObject.Handle.Position = player.Character.LowerTorso.Position
		player.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.TeleportPart.Position)
	end
end)

If you need more info about the script, let me know and I can explain in a bit more detail.
If anyone could please help me or point me in the right direction, that would be greatly appreciated!

3 Likes

Are there any errors in the output?

1 Like

Maybe there’s a typo somewhere.

1 Like

I mean, It works perfectly in studio just not in game.
just wondering why…

1 Like

Try adding workspace:WaitForChild('StartButton') at the start of the local script.

2 Likes

That seemed to do the trick. The script works but only half it, the camera doesn’t attach to the part.

1 Like

add repeat task.wait() until workspace.CurrentCamera after WaitForChild

1 Like

In Studio, press alt + p. Or you can go to file > Publish to roblox

1 Like

I normally do that.

(Character limit)

1 Like

Did you try do that again?

character limit

1 Like

Just so were clear, it should look like:

workspace:WaitForChild('StartButton') repeat task.wait() until workspace.CurrentCamera

or have I messed this up completely?

1 Like

Every single time I add something to the script.

1 Like
workspace:WaitForChild('StartButton')
repeat task.wait() until workspace.CurrentCamera
local Camera = workspace.CurrentCamera
local remote = game.ReplicatedStorage:WaitForChild("Remote")
local value = game.workspace.Values.Walllnut

function onClicked(Player)
	print("ButtonHasBeenPressed")
	remote:FireServer("ToasterIsFired")
	wait(0.1)
	local number = value.Value
	local part = workspace:WaitForChild(number)
	Camera.CameraSubject = part:WaitForChild("Handle")
end
game.workspace.StartButton.ClickDetector.MouseClick:connect(onClicked)
1 Like

This didn’t work. The camera still doesn’t switch over to the part.
I could share a link to the game

1 Like

do /console in-game and check both server and client for red text

1 Like


This is on the client side

1 Like

local part = workspace:WaitForChild(number) is stopping the script

1 Like

You can open the developer console while not in Studio (when you’re on the client instead) just hit F9 to open the console.
You can do this for all games but for games that you don’t have Edit access to will just display your client’s logs instead.

1 Like

Thanks for that!

(Character limit)

change local part = workspace:WaitForChild(number)
to
local part = workspace:WaitForChild(value.Value)