Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Making a interaction script Universal?

Asked by 9 years ago
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.

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? GoldenPhysics 474 — 9y
0
No errors. But I want to check if the magnitude of them and the torso is 2.25 YellowoTide 1992 — 9y
1
Would it be easier to set the brightness to 1000 in studio and alter the enabled property? GoldenPhysics 474 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago
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.

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. YellowoTide 1992 — 9y
0
Nvm not a syntax error :) YellowoTide 1992 — 9y
0
lights is invalidbtw YellowoTide 1992 — 9y
Ad

Answer this question