My code runs perfectly fine in one game, but doesn't in another?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local zone = require(game.ReplicatedFirst:WaitForChild("Zone"))
local bad = zone.new(workspace.Placing.Bad)

local placeRemote = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("PlaceTower")
local runService = game:GetService("RunService")
local contextAction = game:GetService("ContextActionService")
local canPlace = true
local YOffset = 1.4
local maxDist = 25

local defender = game.ReplicatedStorage.Towers.Defender:Clone()
local Display = defender.Folder.Display
Display.Parent = workspace.Placing
defender.Parent = workspace.Placing

player.CharacterAppearanceLoaded:Wait()
mouse.TargetFilter = workspace.Placing

local trackedBad = bad:trackItem(defender.Folder.Hitbox)

local function pingServer(something, inputState) 
	if inputState == Enum.UserInputState.End and canPlace then
		local magnitude = (player.Character.HumanoidRootPart.Position - defender.PrimaryPart.Position).magnitude

		if magnitude <= maxDist then
			local result = placeRemote:InvokeServer(defender:GetAttribute("ToPlace"), mouse.Hit.Position)

			print(result)
		end
	end
end

contextAction:BindAction("Place Tower", pingServer, true, Enum.UserInputType.MouseButton1)

local function changeColor(tower, range, status) 
	local color
	
	if status == true then 
		tower.Color.Value = true
		color = BrickColor.new("Really red")
	else
		tower.Color.Value = false
		color = BrickColor.new("Bright green")
	end
	
	for i,v in ipairs(tower:GetDescendants()) do 
		if v:IsA("BasePart") then 
			v.BrickColor = color 
		end
	end
	
	range.BrickColor = color

end

bad.itemEntered:Connect(function(trackedBad)
	changeColor(defender, Display, true)
	canPlace = false
end)

bad.itemExited:Connect(function(trackedBad)
	changeColor(defender, Display, false)
	canPlace = true
end)

runService.Heartbeat:Connect(function()
	local mousePos = mouse.Hit.Position
	local magnitude = (player.Character.HumanoidRootPart.Position - defender.PrimaryPart.Position).magnitude
	
	defender:SetPrimaryPartCFrame(CFrame.new(mousePos.X, mousePos.Y + YOffset, mousePos.Z))
end)

I have a really strange issue. The above code was written in one game, which is a child of another game. To get to that game, I have to launch the parent game, then open asset manager, then I can finally launch the game this code is in. I don’t want to have to do this every time, so I thought what if I just publish this as a seperate game for easy access. The above code runs perfectly fine in game A, however in game B this doesn’t run at all, no errors whatsoever. What do I do?

Has happened to me a lot. Did you make sure you have your checks to wait for everything to actually load in?

maybe the new game has things interfering, or isnt setup properly?

  • a script that disables all other scripts
  • an infinite yield that you may be ignoring because it isnt an error
  • this local script is disabled
  • this local script isnt placed where local scripts can run

The game was made by pressing “Publish to Roblox As”, so it should be a direct clone

I dont believe so, no. They both should be exact clones of eachother

Try adding checks and waiting for everything. Then try.

it sounds like it should be working, I’m not too sure what else could be the issue
maybe the new game is missing assets that are used in the Zone class? I’d expect errors to appear though

maybe try using breakpoints/prints and stepping through the code to see what is or isnt working properly

Sorry for the delay, I had to eat. I added a bunch of checks, and for some reason it freezes at the player.CharacterAppearanceLoaded:Wait() line. I dont know why as my character is loaded in perfectly fine and I am able to interact, move around with my animations and character and everything.

maybe the issue is that the character is done loading before you start waiting for the character to finish loading

idk if there’s anything build in to wait for the character to finish loading, so maybe do something like
local character = player.Character or player.CharacterAdded:Wait()
it’s not appearance loaded, but I don’t think you need to wait for the appearance to load

It worked :smiley:
I never knew this could happen lol, ill be sure to keep this in mind for future projects. Thanks