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.
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.
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.
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.
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.
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))
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?
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))
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))