Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you change the ambient for a local player if the player hits a part?

I want to change the ambient for my game if a player enters a cave or mine shaft. My idea for this was to have the player hit a part that would change the lighting to be dark just for them (not the whole server). I know how to make the whole server change the ambient using just a scirpt:

function onHit(hit)
		game.Lighting.Ambient = Color3.new(0,0,0)
		game.Lighting.ShadowColor = Color3.new(0,0,0)
end

script.Parent.Touched:connect(onHit)

However I don't know how to do it with a local script. Please help me and show me how to do it.

2 comments

0

Not sure if this is the website for this.

0

what do you THINK this websites for?

3 answers

It is possible, with localscripts. Try writing it in a localscript and then test it.

Create a LocalScript in StarterPlayerScripts (so it will only run once when the player joins the game rather than every time they respawn).

You will need to get the path to the brick you want to listen to Touched events for, and then setup the event. When something touches that part, you simply want to know if its parent is the local player's character. If so, then run the code you want.

local Lighting = game.Lighting
local localPlayer = game.Players.LocalPlayer

workspace.ModelNameHere.PartNameHere.Touched:Connect(function(p)
	if p.Parent ~= localPlayer.Character then return end
	Lighting.Ambient = Color3.new(0,0,0)
	Lighting.ShadowColor = Color3.new(0,0,0)
end)	

(Obviously replace ModelNameHere.PartNameHere with the path to the part.)

(The purpose of local Lighting = game.Lighting was just so you don't have to say game.Lighting over and over again - it's faster for you to type in and for Roblox to run.)

You can extend this script with more Touched connections to handle other lighting changes as well.

If you wanted the lighting to reset every time the character respawned, you'd probably want to put this LocalScript in StarterCharacterScripts instead and to change the Lighting near the beginning of the script (outside of any Touched event listeners).

This discussion is preserved as it was. New answers, comments, and votes are closed.