Script does not run correctly at startup

Hi, I’m really tired of this topic and I’ll try to explain the best I can. The following LocalScript is used to detect values ​​of a stringValue and it depends on the stringvalue, it changes the quality of the shadows but it does not detect them or anything, I have other scripts the same but those if it detects them. However, with a normal script if it works or at least in the serverScriptService datastore.

local Lighting = game:GetService("Lighting")

local StringValueShadow = game.Players.LocalPlayer:WaitForChild("Graficos"):WaitForChild("Shadow")

StringValueShadow:GetPropertyChangedSignal("Value"):Connect(function()
	if StringValueShadow.Value == "Bajo" then
		for _, item in ipairs(workspace:GetDescendants()) do
			if item:IsA("PointLight") then 
				Lighting.GlobalShadows = false
				Lighting.ShadowSoftness = 1
				item.Shadows = false
				item.Brightness = 0.35
				print("Sombras en bajo")
			end
		end
	elseif StringValueShadow.Value == "Medio" then 
		for _, item in ipairs(workspace:GetDescendants()) do
			if item:IsA("PointLight") then 
				Lighting.GlobalShadows = true
				Lighting.ShadowSoftness = 0.5
				item.Shadows = false
				item.Brightness = 0.35
				print("Sombras en medio")
			end
		end
	elseif StringValueShadow.Value == "Alto" then 
		for _, item in ipairs(workspace:GetDescendants()) do
			if item:IsA("PointLight") then 
				Lighting.GlobalShadows = true
				Lighting.ShadowSoftness = 0.1
				item.Shadows = true
				item.Brightness = 0.75
				print("Sombras en alto")
			end
		end
	end
	return
end)

A few questions-

  • Does your :WaitForChild() ever return a value?
  • Is :GetPropertyChangedSignal() ever firing?
  • Where is this Local Script parented to?

P.S. with value objects, you can use .Changed instead of :GetPropertyChangedSignal for shorthand.

1 Like

@Soybeen’s checklist is where your problem lies, as I can confirm the code compiles and functions correctly so long as everything is where it needs to be. One other suggestion I would make on the code aspect would be to move your Lighting changes outside of the loops that change the individual lights, like this:

if StringValueShadow.Value == "Bajo" then
	Lighting.GlobalShadows = false
	Lighting.ShadowSoftness = 1
	for _, item in ipairs(workspace:GetDescendants()) do
		if item:IsA("PointLight") then 
			item.Shadows = false
			item.Brightness = 0.35
			print("Sombras en bajo")
		end
	end
elseif StringValueShadow.Value == "Medio" then 
	Lighting.GlobalShadows = true
	Lighting.ShadowSoftness = 0.5
	for _, item in ipairs(workspace:GetDescendants()) do
		if item:IsA("PointLight") then 
			item.Shadows = false
			item.Brightness = 0.35
			print("Sombras en medio")
		end
	end
elseif StringValueShadow.Value == "Alto" then 
	Lighting.GlobalShadows = true
	Lighting.ShadowSoftness = 0.1
	for _, item in ipairs(workspace:GetDescendants()) do
		if item:IsA("PointLight") then 
			item.Shadows = true
			item.Brightness = 0.75
			print("Sombras en alto")
		end
	end
end
1 Like

We could go one step further with this and make a dictionary filled with lighting properties using your special strings as keys.

local lightingProfiles = {
    ["Bajo"] = {
        ["PointLight"] = {
            ["Shadows"] = false,
            ["Brightness"] = 0.35,
        },
        ["Lighting"] = {
            ["GlobalShadows"] = false,
            ["ShadowSoftness"] = 1,
        },
    },

    ["Medio"] = {
        ["PointLight"] = {
            ["Shadows"] = false,
            ["Brightness"] = 0.35,
        },
        ["Lighting"] = {
            ["GlobalShadows"] = true,
            ["ShadowSoftness"] = 0.35,
        },
    },

    ["Alto"] = {
        ["PointLight"] = {
            ["Shadows"] = true,
            ["Brightness"] = 0.75,
        },
        ["Lighting"] = {
            ["GlobalShadows"] = true,
            ["ShadowSoftness"] = 0.1,
        },
    },
}

local Lighting = game:GetService("Lighting")
local stringValue = -- define your stringValue here

stringValue.Changed:connect(function(newValue)
    for property,value in next,lightingProfiles[newValue]["Lighting"] do
        Lighting[property] = value
    end
    for _,obj in next,workspace:GetDescendants() do -- it would be better if you compiled all your PointLights into table, to avoid massive iteration through everything in Workspace
        if obj:IsA("PointLight") then
            for property,value in next,lightingProfiles[newValue]["PointLight"] do
                obj[property] = value
            end
        end
    end
end)
1 Like

I really appreciate the fact that you help me since no one helped me with this problem, but I get these bars …


I’m sorry if I didn’t respond soon but it’s because I lost faith that they would help me, I thought no one would, thank you.

I forgot to put commas in the appropriate places, sorry :slight_smile:
Let me go back and edit that code.

In your original code, the problem was this line only:

local StringValueShadow = game.Players.LocalPlayer:WaitForChild("Graficos"):WaitForChild("Shadow")

Your other code was actually fine. It’s just this was never finding Graficos, or Shadow. Make sure they exist at some point.

ah i already put it to the test and it really only works rarely in roblox studio, it works if i change value from another script

You say it works only some of the time, not all of the time, which makes me think that this StringValue needs to moved to a more reliable directory.

Instead of using .Changed on a StringValue at all, perhaps you could use RemoteEvents to tell the client when they should change lightingProfiles.
Here’s how to use RemoteEvents: Bindable Events and Functions | Roblox Creator Documentation

2 Likes

I think it should almost be done but in your part of the script I get an error Workspace.NUTRICORP.Graficos: 4: attempt to index nil with 'Lighting

local RemoteEventShadow = ReplicatedStorage.Graficos:WaitForChild("FireShadowChangeScript")

local function ChangeShadowSingal(newValue)
	for property,value in next,lightingProfiles[newValue]["Lighting"] do
		Lighting[property] = value
	end
	for _,obj in next,workspace:GetDescendants() do -- it would be better if you compiled all your PointLights into table, to avoid massive iteration through everything in Workspace
		if obj:IsA("PointLight") then
			for property,value in next,lightingProfiles[newValue]["PointLight"] do
				obj[property] = value
			end
		end
	end
end

RemoteEventShadow.OnClientEvent:Connect(ChangeShadowSingal)

It looks like the value sent along your RemoteEventShadow is not “Bajo”, “Medio”, or “Alto”. It must be one of those three strings in order to properly index the lightingProfiles table.

1 Like

My God, without you I couldn’t have done it. Thanks.

1 Like