Script works fine in studio, but doesn't work in game

I’m using a lighting zone script. it works fine in studio but doesn’t work in-game

I don’t understand what the heck is going on.

script:


print("@")
local player = game.Players.LocalPlayer


local triggers = game.Workspace:WaitForChild("EnvironmentTriggers")
local default1 = triggers:WaitForChild("Default1")


local Settings = script.Parent:WaitForChild("Settings")
local default = script.Parent:WaitForChild("DefaultProperties")
local modify = require(script:WaitForChild("ModifyProperties"))
	print("1")
modify.ApplyDefaults() --// Begin by initializing default settings

player.CharacterAdded:Connect(function(character)
	modify.ApplyDefaults() --// If value is true, reset lighting on spawn
	if Settings:WaitForChild("ApplyDefaultOnSpawn").Value == true then
		modify.ApplyDefaults() --// If value is true, reset lighting on spawn
	end
		print("2")
	local root = character:WaitForChild('HumanoidRootPart') --// Compatable with both R15 and R6
		print("3")
	root.Touched:Connect(function(trigger)
		if trigger.Parent == triggers then --// If hit part is a trigger
			modify.Update(trigger)
				print("4")
		end
	end)
end)

The script stops at “1” in-game.

HEKOP

can anybody help?

1 Like

Update: I changed nothing and it worked once, then stopped working when I rejoined.

1 Like

Gonna bump this because i really need this to be fixed

It’s possible the character is being added before the connection, causing the rest of the script to not run. You should do a check for if the character is already added after the connection, and turn the code inside the connection into a function which can be used in both cases.

1 Like

Force the character to load again.
Also you might wanna disconnect this function to prevent making infinite connections

1 Like

How would I do that? I’m pretty bad at scripting.

Can you post the module script?

If it is stopping after printing 1, then the next thing is the module.

local modify = {}

local tweenService = game:GetService("TweenService")
local default = script.Parent.Parent:WaitForChild("DefaultProperties")

function modify.Update(trigger, transitionOverride)
	for _,v in pairs(default:GetChildren()) do
		if not trigger:FindFirstChild(v.Name) then
			v:Clone().Parent = trigger --// If trigger doesn't have default values, give it them
		end
	end
	
	local tweenInfo = TweenInfo.new(transitionOverride or trigger.TRANSITION.Value) --// The tween info for the transition time, will be reused
	
	for _,value in pairs(trigger:GetChildren()) do
		local modifiedProperties = {}
		
		local succ, err = pcall(function()
			if value.Name ~= 'TRANSITION' and value.Name ~= 'Atmosphere' then
				local goal = {[value.Name] = value.Value}
				local tween = tweenService:Create(game.Lighting, tweenInfo, goal)
				tween:Play()
				
				table.insert(modifiedProperties, value.Name)
			end
		end)
		
		if not succ then
			warn(err)
		end
		
		if value.Name == 'Atmosphere' then
			for _,atmospheric in pairs(value:GetChildren()) do
				local goal = {[atmospheric.Name] = atmospheric.Value}
				local tween = tweenService:Create(game.Lighting.Atmosphere, tweenInfo, goal)
				tween:Play()
			end
		end
	end
end

function modify.ApplyDefaults()
	modify.Update(default, 0)
end

return modify

I can only suggest adding some print statements to you module:


local modify = {}

local tweenService = game:GetService("TweenService")
local default = script.Parent.Parent:WaitForChild("DefaultProperties")

function modify.Update(trigger, transitionOverride)
	
	print("trigger, transitionOverride =", trigger, transitionOverride)
	
	for _,v in pairs(default:GetChildren()) do
		
		print("v =", v)
		
		if not trigger:FindFirstChild(v.Name) then
			
			print("trigger not found", v.Name)
			
			v:Clone().Parent = trigger --// If trigger doesn't have default values, give it them
		end
	end

	local tweenInfo = TweenInfo.new(transitionOverride or trigger.TRANSITION.Value) --// The tween info for the transition time, will be reused

	for _,value in pairs(trigger:GetChildren()) do
		
		local modifiedProperties = {}

		local succ, err = pcall(function()
			if value.Name ~= 'TRANSITION' and value.Name ~= 'Atmosphere' then
				local goal = {[value.Name] = value.Value}
				local tween = tweenService:Create(game.Lighting, tweenInfo, goal)
				tween:Play()

				table.insert(modifiedProperties, value.Name)
				
				print("success")
				
			end
		end)

		if not succ then
			warn(err)
		end

		if value.Name == 'Atmosphere' then
			
			print("Atmosphere")
			
			for _,atmospheric in pairs(value:GetChildren()) do
				local goal = {[atmospheric.Name] = atmospheric.Value}
				local tween = tweenService:Create(game.Lighting.Atmosphere, tweenInfo, goal)
				
				print("goal =", goal)
				
				tween:Play()
			end
		end
	end
end

function modify.ApplyDefaults()
	modify.Update(default, 0)
end

return modify

After a lot of tinkering, I found the issue and have fixed it. Thank you to everybody who responded.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.