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

Making a interaction script Universal?

local lights = game.Workspace.Interactables.Switches:GetChildren()

function keydown(k)
	if k:lower() == "e" and ((script.Parent.Parent.Character.Torso.Position - lights.Position).magnitude) < 2.25 then
		for a, aa in pairs(game.Workspace.Interactables.Lights:FindFirstChild(lights.Name):GetChildren())  do
			if aa.Light.Brightness == 0 then
				aa.Light.Brightness = 1000
			else
				aa.Light.Brightness = 0
			end
		end
	end
end

local m = script.Parent.Parent:GetMouse()
m.KeyDown:connect(keydown)

What I'm trying to do is well. I have a lot of light switches and I'm trying to avoid using a billion scripts (one in each lightswitch) and use one local script in starter pack. I'm not sure how to make it work with every light switch? And also. This script does NOT work. and when it was working with one lightswitch it did NOT work online.

3 comments

1

You seem to be getting the position of the table "lights". Do you mean to try each one? Also, are there any errors given?

0

No errors. But I want to check if the magnitude of them and the torso is 2.25

1

Would it be easier to set the brightness to 1000 in studio and alter the enabled property?

1 answer

local interactables = game.Workspace.Interactables

function keydown(k)
	if k:lower() == "e" then
		for i, v in pairs(interactables.Switches:GetChildren()) do
			if game.Players.LocalPlayer:DistanceFromCharacter(v) < 2.25 then
				for k, va in pairs(interactables.Lights:FindFirstChild(lights.Name):GetChildren())  do --I'm assuming "lights.Name" is valid
					va.Light.Enabled = not va.Light.Enabled --set the brightness to 1000 in studio
				end
				break --if it changes the lights, don't keep looking for a switch close to the player.
			end
		end
	end
end

local m = game.Players.LocalPlayer:GetMouse() --stays intact in case the ancestry changes, assuming its a local script
m.KeyDown:connect(keydown)

Feel free to comment with questions and problems as I have not tested this. If it helps, please accept, and/or vote up.

3 comments

0

No output. The lights that will change (amounts change not always the same) are located at game.Workspace.Interactables.Lights.[Insertname of switch activated here}. The name of the lights depends on the switch activated. There is a syntax error in line 7 should be Interactables.

0

Nvm not a syntax error :)

0

lights is invalidbtw

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