Local light creation?

I am trying to make a script which will locally create a light inside the Torso of the player when it is night. It seems to be running fine it just won’t add in the light. I get no errors. Anyone know how to do this?

1 Like

Where’s the script then? You should insert it here if you want to get fixed it.

local player = game.Players.LocalPlayer

while true do

	if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then -- is day
		if player.Character.Torso:FindFirstChild("Light") then
			local light = player.Character.Torse.Light
			if light.Range == 0 then
				tween = game:GetService("TweenService"):Create(light, TweenInfo.new(1), {Range = 21})
			end
		else
			local light = Instance.new("PointLight")
			light.Name = "Light"
			light.Brightness = 15
			light.Color = Color3.fromRGB(38,38,38)
			light.Range = 0
			light.Parent = player.Character.Torso
		end
	else
		if player.Character.Torso:FindFirstChild("Light") then
			local light = player.Character.Torso.Light
			tween = game:GetService("TweenService"):Create(light, TweenInfo.new(1), {Range = 0})
			light:Destroy()
		end
	end

	wait(1)
end

It seems like a typo here. Can you try fixing it?

I think setting the range to 0 is what causes it.

Well, it tweens the range so it shouldn’t cause it.

local player = game.Players.LocalPlayer

while true do
	print "scanned"
	if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then -- is day
		print "its day"
		if player.Character.Torso:FindFirstChild("Light") then
			local light = player.Character.Torso.Light
			if light.Range == 0 then
				tween = game:GetService("TweenService"):Create(light, TweenInfo.new(1), {Range = 21})
			end
		else
			print "added light"
			local light = Instance.new("PointLight")
			light.Name = "Light"
			light.Brightness = 15
			light.Color = Color3.fromRGB(38,38,38)
			light.Range = 5
			light.Parent = player.Character.Torso
		end
	else
		if player.Character.Torso:FindFirstChild("Light") then
			print"removed light"
			local light = player.Character.Torso.Light
			tween = game:GetService("TweenService"):Create(light, TweenInfo.new(1), {Range = 0})
			light:Destroy()
		end
	end

	wait(1)
end

still doesn't work

It seems to only be printing the “scanned”, and not anything else.

I’ll DM you a fixed copy in a few minutes with some explanations of what’s not working and why.

Fixed script
local character = script.Parent
local rootPart = character.PrimaryPart

local light = Instance.new("PointLight")
light.Name = "Light"
light.Brightness = 20
light.Color = Color3.fromRGB(38,38,38)
light.Range = 0
light.Parent = rootPart

local tween

while true do
	print("scanned")
	if game.Lighting.ClockTime >= 18 or game.Lighting.ClockTime <= 6 then -- is night
		print("is night")
		if light.Range < 21 then
			if tween then
				tween:Destroy()
			end
			tween = game:GetService("TweenService"):Create(light, TweenInfo.new(1), {Range = 21})
			tween:Play()
			tween.Completed:Wait()
		end
	else
		print("is day")
		if light.Range > 0 then
			if tween then
				tween:Destroy()
			end
			tween = game:GetService("TweenService"):Create(light, TweenInfo.new(1), {Range = 0})
			tween:Play()
			tween.Completed:Wait()
		end
	end

	wait(1)
end
Short explanation (longer one in DM)

Existence checks so it doesn’t error. Call :Play on the tween. Use < and > instead of ==.

1 Like