Help with a local lighting change script

So I have this code that should be changing the players lighting because they own a perk called “VisionIncrease”

I feel like this should be pretty simple but i still cant seem to figure it out.

This script is located inside of StarterPlayerScripts

local player = game.Players.LocalPlayer

local Perk = "VisionIncrease"

if player == script.Parent.Parent then

	player.ChildAdded:Connect(function(child)
		
		if child.Name == Perk and child:FindFirstChild("VisionIncrease").Value == true then
			
			game.Lighting.FogEnd = 1000
			
			print("Lighting Changed")
			
		end
	
	end)
	
end

I don’t know how else to go at this because I’ve tried so many different ways

1 Like

I tried multiple things with this script right now and it seems like one of them worked. I am being honest, I don’t know why my version of this script works just by moving some lines of script, but it should work when you move this: …

… to the place where the child isn’t added yet. For example like this:

… So the child must be added after the ChildAdded Part and NOT after it like this one:

I guess the script must firstly know, that it needs to check if a child will get added, before actually adding it. A script goes down line by line you know? And that might be the reason. It doesn’t get to read the ChildAdded Part firstly in your script probably.

btw: you don’t need to check this inside your script:
image
it doesn’t really make sense because there can be no one else using your localscript, so there can be only one player.

NOTE:
Nevermind. I think I understood it wrong. I don’t think you can move your script like I described it, because I didn’t think about that there might be a diffrent script, which adds the child. Well, if that’s the case, then I can try explaining you how you could do that.

So, I think I may have kinda messed up with my solution above. Could you maybe show the part of any scripts, where the child gets added and tell, which type of script it is? (But you could still take a look at my solution. Maybe it could still help if I didn’t get it wrong.)

So let me explain everything,

In a local script, a player purchases a perk called “VisionIncrease”
this local script then fires a RemoteEvent to a server script that edits the player’s “leaderstats” (in this case, Gems) and then also edits a value called VisionIncrease. This value is inside of a folder named Perks which inside of the player.

So the local fires the server and the server fires a different local script which was the script I originally posted. Am I over complicating this? I feel like there is a much more simple way to do this but regardless, when my local script is fired, nothing happens.

Thank you for the clarification, I honestly dont know why i even put that in now that i think about it :joy:

Oh my goodness I just figured it out, here is the revised script,

(I just had to give the script time for everything to load so you were right @PVP_Player2373902 )

local player = game.Players.LocalPlayer

local Perk = "VisionIncrease"

player.ChildAdded:Connect(function(child)
	
	print(child)
		
	if child.Name == "Perks" then
		
		if child:WaitForChild(Perk) then --this was the problem i believe, i had it as find not wait
			
			print(child:FindFirstChild(Perk).Value)
			
			game.Lighting.FogEnd = 1000

			print(game.Lighting.FogEnd)
			
		end
			
		end
	
end)
1 Like

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