Hey! I am having trouble with this script. I am trying to make it so that when you step on the part the fog end changes. And when you step off of it the lighting settings return to the original settings.
But it does nothing and i dont get any output from the output can you please help me?
local part = script.Parent
local function onTouch(part)
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 80
end
local function onTouchEnded(part)
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 30
end
So first off, i recommend you use the code tagger, so it puts it as code,
to the point of your code, basically, touched fires any time any part of the player, or any object touches the part, you may need to add a debounce but it gets rather complicated cause if anybody else touches it it’ll still fire off, as it’s per-event activation
As far as I’m aware, you cannot change the lighting properties from a Script, but rather a LocalScript. However, LocalScripts cannot use :Touched.Connect(function() functions.
I’ve tried changing Lighting from a regular script before, and it only worked in studio, and not in game. I’m not a scripter, so don’t take my word for it exactly, this is just what happened to me when I experienced the same issue.
I recommend you try and find a way to make this change locally only, but if you’re wanting to make this happen to everyone, then you’ll need to get assistance from someone else as I don’t have a clue.
You can change it via a script actually, most day/night cycle systems use it, I believe however, it may need to be in server script service in order to function, but don’t quote me on that
Oh yeah! I think I heard something about some scripts NEEDING to be in ServerScriptService for it to work, as it runs scripts from the ServerSide, and not both sides.
Maybe try this for your ServerScriptService script, @SOLo470.
local part = workspace.WHATEVERYOURPARTSNAMEISHERE
local function onTouch(part)
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 80
end
local function onTouchEnded(part)
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 30
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
Like I said, I’m not a great and full blown scripter, but I think you can call these Functions by having your script inside ServerScriptService, as well as having your Part in workspace. Try this method, and post here if it works or not.
Hey, yes i know that the best way to execute this would to be in a local script but i dont know if the local script can even run a touched event. So maybe i will just end up trying a new way to do it…
Also try using the script above that I posted. It’s your script, but with the variable changed. Now, just place the script in ServerScriptService, and if you step or touch the part, it should solve your lighting problem, but you might want to add some sort of cool down to it, so it doesn’t fire off a million times. In some situations script.Disabled = true can help, along with debounce() I believe. I don’t really know but yeah. Trying my best here.
I can try this in studio, and see if I can find a solution for you later today if you don’t end up succeeding by then ^^ I’ll let you know if I can find anything, but I have school now.
Touched events work just fine in LocalScripts. But if @SOLo470 is putting the code in a LocalScript, and the LocalScript in the Part, then it will not run at all, because LocalScripts need to run from somewhere related to the player.
The script works when it is in a Script inside a Part, but it will be very bad-looking! Sure, a part hitting it will make the fog go farther. But regardless of how many parts are currently touching, any one of them leaving will set the fog back.
The solution is to keep track of how many parts are touching, and not reset the fog until all parts are gone.
local part = script.Parent
-- amount of parts touching this part
-- fog must be close when it is 0 and far when it is anything but 0
local numTouching = 0
local function onTouch(part)
if numTouching == 0 then -- if this is the first part touching, set the fog farther
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 80
end
numTouching = numTouching + 1
end
local function onTouchEnded(part)
if numTouching == 1 then -- if this is the last part touching, set the fog closer
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 30
end
numTouching = numTouching - 1
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
The other problem here is that the fog will change for all players.
To make it only work for the current player, first put it in a LocalScript, and the LocalScript in somewhere that it can run, and where it won’t be deleted each time the player respawns.
Then change part to be where the part is in the game.
Then make the fog only change when it is touching the current character. There are a myriad ways this can go wrong, such as a part exiting the character, so the safest bet is to make the part non-CanCollide and 5 studs tall (so the script detects the player inside), and make it only detect the HumanoidRootPart of the character.
Adapted to work as a LocalScript in StarterPlayer.StarterPlayerScripts
local part = workspace.Part
-- amount of parts touching this part
-- fog must be close when it is 0 and far when it is anything but 0
local numTouching = 0
local function isThisMe(part)
-- is this the current player's character's HumanoidRootPart?
return part.Name == "HumanoidRootPart" and game.Players:GetPlayerFromCharacter(part.Parent) == game.Players.LocalPlayer
end
local function onTouch(part)
if not isThisMe(part) then return end -- do nothing if this isn't the character
numTouching = numTouching + 1
if numTouching == 1 then -- if this is the first part touching, set the fog farther
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 80
end
end
local function onTouchEnded(part)
if not isThisMe(part) then return end -- do nothing if this isn't the character
numTouching = numTouching - 1
if numTouching == 0 then -- if this was the last part touching, set the fog closer
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 30
end
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
No, I figured out his error, that doesn’t need to make him use this Local Script method. He didn’t make it so when a Humanoid is detected when the part is touched to do an action. All he is calling is the Touched function, but what needs to touch the part for Touched to work? He didn’t add that. Somelike hit.Parent and such I believe will fix this problem as I’ve seen in other scripts, I just don’t fully remember what it is. Sorry if this doesn’t help.
I think I have a solution, however there are left over errors in his script, cuz I don’t think it’s supposed to produce fog straight away, or keep it, but it does enable and disable the fog if you step on and off of it.
local Part = workspace.LightPartChanger
local function onTouch(Part)
local Parent = Part.Parent
if game.Players:GetPlayerFromCharacter(Parent) then
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 80
end
end
local function onTouchEnded(Part)
game.Lighting.FogStart = 0
game.Lighting.FogEnd = 30
end
Part.Touched:Connect(onTouch)
Part.TouchEnded:Connect(onTouchEnded)
This still has the same problems as what I pointed out. If two players step on the part, then the fog will clear after one of them leaves, even if another is still on it. The fog will apply to all players, which I assume wasn’t what the OP wanted because they had likely put it in a LocalScript. It is great that you tested it and made a video of it, though!
He could make it so the script disables itself when someone touches it, preventing another user from walking to the part, and walking off. I’d need to get a friend or alt to test this.
also when the original person steps off, then it will be accessible again