How would I turn a nightvision script off when switching morphs?

  1. What do you want to achieve?
    Hi! I have this nightvision script, and I want it to go back to normal when the player switches morphs.

  2. What is the issue?
    Here is my script!

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local nightvision = false

function onKeyPress(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin and nightvision == false then
		game.Lighting.Ambient = Color3.new(0.623529, 0.596078, 0.721569)
		nightvision = true
		wait()
	elseif userInputState == Enum.UserInputState.Begin and nightvision == true then
		game.Lighting.Ambient = Color3.new(0.32549, 0.247059, 0.364706)
		nightvision = false
	end
end

game.ContextActionService:BindAction("NightVision", onKeyPress, true, Enum.KeyCode.N)
game.ContextActionService:SetPosition("NightVision", UDim2.new(.5,0,-.1,0))

When a player switches morphs, the ambient lighting stays in the nightvision state. Only this specific kind of morph can use nightvision.

  1. What solutions have you tried so far?
  • I don’t want to disable the script, I’d prefer to change the lighting back.
  • I’ve tried using this page, since I thought that when the player switches morphs it also kills them, which is apparently not true.
  • I’m thinking if I can detect when the CFrame changes or is destroyed, THEN it changes the lighting back.

Thanks in advance! I’m getting better at making my posts shorter.

You could set the ambient when changing morphs and then adjust your script.

Current:
and nightvision == false

Proposed:
and game.Lighting.Ambient == Color3.new(0.32549, 0.247059, 0.364706)

This would allow you to change it in different local scripts and not have to parse that you changed it to the original script.

I think I understand. You’re suggesting I set the ambient to the nightvision ambient as soon as the morph is activated, then I get a little lost there. Nightvision should be a toggle, which is why the script changes the ambient from the original to the brighter one for the specific morph/player. I’m confusing myself a little bit.

This is what the nightvision script looks like in action. This happens when I press N, the brighter one is when it’s activated.

How does the player change morphs?

The game fires a remoteEvent in ReplicatedStorage that destroys the PrimaryPartCFrame of the original avatar and replaces it with my model’s primary part. Is there a way I can detect when that primary part isn’t associated with the character? Or is that a hacky way to do it? I’m a super beginner scripter, and I mostly rely on this forum and tutorials to get my games working.

local event = game.ReplicatedStorage.ChangeChar

event.OnServerEvent:Connect(function(player,model)
	local Char = model:Clone()
	local Pos = player.Character:GetPrimaryPartCFrame()
	player.Character:Destroy()
	player.Character = Char
	Char.Parent = workspace
	Char:SetPrimaryPartCFrame(Pos)
end)

I can’t exactly remember where I got this particular script from.

You should set the ambience back to normal in the local script where you fire the remote event.

1 Like

OH good idea. I didn’t want to use other scripts to do it, but honestly, I may as well use the script that’s gonna run regardless. I’ll try it out, then I’ll update this reply.

local Player = game.Players.LocalPlayer
local nightvision = false
local Character = Player.Character or Player.CharacterAdded:Wait()
Character:GetPropertyChangedSignal("Parent"):Connect(function() -- disable nightvision when character is destroyed
	if Character.Parent == nil then
		game.Lighting.Ambient = Color3.new(0.32549, 0.247059, 0.364706)
		nightvision = false
	end
end)
local Humanoid = Character:WaitForChild("Humanoid")


function onKeyPress(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin and nightvision == false then
		game.Lighting.Ambient = Color3.new(0.623529, 0.596078, 0.721569)
		nightvision = true
		wait()
	elseif userInputState == Enum.UserInputState.Begin and nightvision == true then
		game.Lighting.Ambient = Color3.new(0.32549, 0.247059, 0.364706)
		nightvision = false
	end
end


game.ContextActionService:BindAction("NightVision", onKeyPress, true, Enum.KeyCode.N)
game.ContextActionService:SetPosition("NightVision", UDim2.new(.5,0,-.1,0))

Hm, that didn’t seem to work. When I add the game.Lighting property, the script now looks like this:

local event = game.ReplicatedStorage.ChangeChar

event.OnServerEvent:Connect(function(player,model)
	local Char = model:Clone()
	local Pos = player.Character:GetPrimaryPartCFrame()
	player.Character:Destroy()
	player.Character = Char
	Char.Parent = workspace
	Char:SetPrimaryPartCFrame(Pos)
	game.Lighting.Ambient = Color3.new(0.32549, 0.247059, 0.364706)
end)

Is anything misplaced?

This didn’t seem to work, either. I’m not sure what the issue is with it, the script looks pretty solid! I didn’t change anything, should I have done that?

No, you do it in the local script, not the server script, that will affect everyone in the server.

1 Like

It works for me? send a place file that I can open in studio

OHH I see. Let me try again, this time with the ambient lighting change in the local script. I totally forgot that it would affect everyone.

Alright! I’ll make a new baseplate in a second. It might be a bit different from yours, given that my models are custom.

Why do you need a place file? Everything is here.

It could be that the character’s parent gets set quickly with your morph and causes it to not disable nightvision, try taking out the if statement and resetting night vision whenever the character’s parent is changed rather than when its changed and the parent is nil

local Player = game.Players.LocalPlayer
local nightvision = false
local Character = Player.Character or Player.CharacterAdded:Wait()
Character:GetPropertyChangedSignal("Parent"):Connect(function()
	game.Lighting.Ambient = Color3.new(0.32549, 0.247059, 0.364706)
	nightvision = false
end)
local Humanoid = Character:WaitForChild("Humanoid")


function onKeyPress(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin and nightvision == false then
		game.Lighting.Ambient = Color3.new(0.623529, 0.596078, 0.721569)
		nightvision = true
		wait()
	elseif userInputState == Enum.UserInputState.Begin and nightvision == true then
		game.Lighting.Ambient = Color3.new(0.32549, 0.247059, 0.364706)
		nightvision = false
	end
end


game.ContextActionService:BindAction("NightVision", onKeyPress, true, Enum.KeyCode.N)
game.ContextActionService:SetPosition("NightVision", UDim2.new(.5,0,-.1,0))
1 Like

That worked! Luckily I only have 11 morphs to add this line of code to. Thank you!

1 Like

That didn’t change much for some reason. IEnforce_Lawz’s suggestion worked, but thank you for trying, anyway!!

ok, np. here is the final script I had for you if you wanna give it a shot

local Player = game.Players.LocalPlayer
local nightvision = false


local function SetNightVision(x)
	if x == nil then x = false end
	game.Lighting.Ambient = (x == false) and Color3.new(0.32549, 0.247059, 0.364706) or Color3.new(0.623529, 0.596078, 0.721569)
	nightvision = x
end


local Character = Player.Character
if Character then
	Character:GetPropertyChangedSignal("Parent"):Connect(function()
		SetNightVision(false)
	end)
end
Player.CharacterAdded:Connect(function(Char)
	Char:GetPropertyChangedSignal("Parent"):Connect(function()
		SetNightVision(false)
	end)
end)


function onKeyPress(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin and nightvision == false then
		SetNightVision(true)
	elseif userInputState == Enum.UserInputState.Begin and nightvision == true then
		SetNightVision(false)
	end
end


game.ContextActionService:BindAction("NightVision", onKeyPress, true, Enum.KeyCode.N)
game.ContextActionService:SetPosition("NightVision", UDim2.new(.5,0,-.1,0))
1 Like

That also didn’t work with my models, sorry! It must be because of the way mine are structured. I appreciate the help, regardless!

1 Like