This script is a simple lightswitch script, it can be used to switch on and off lights in the same script, or instead of HideLight() ShowLight() you could use spawn to, for example Spawn and Hide entities, like a plank used for a bridge, could be up or down etc.
Both switch states use PlaySound(e,1) but you could change one state to PlaySound(e,0) for a different switch sound for off or on.
The player can only use the switch when facing it.
I have also used the timer to slow the switch on/off times as the script was so fast. Simple but it works
-----------------------------------
-- lightstate = 0/off
-- lightstate = 1/on
-- Uses the timer in line 17 to reset keydown to 0.
local lightstate = 0
-- checks if E is pressed.
local keydown = 0
-- player distance
local dist = 50
function switches_init(e)
end
function switches_main(e)
PlayerDist = GetPlayerDistance(e)
if keydown == 1 and GetTimer(e)>150 then
keydown = 0
end
if PlayerDist < dist and g_PlayerHealth > 0 then
SourceAngle = g_PlayerAngY
while SourceAngle < 0.0 do
SourceAngle = SourceAngle + 360.0
end
while SourceAngle > 340.0 do
SourceAngle = SourceAngle - 360.0
end
PlayerDX = (g_Entity[e]['x'] - g_PlayerPosX)
PlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ)
DestAngle = math.atan2( PlayerDZ , PlayerDX )
-- Convert to degrees
DestAngle = (DestAngle * 57.2957795) - 90.0
Result = math.abs(math.abs(SourceAngle)-math.abs(DestAngle))
if Result > 180 then
Result = 0
end
if Result < 20.0 then
if lightstate == 0 and PlayerDist < dist then
PromptLocal (e,"Press E to turn on the light.")
end
if lightstate == 1 and PlayerDist < dist then
PromptLocal (e,"Press E to turn off the light.")
end
if g_KeyPressE == 1 and lightstate == 1 and keydown == 0 then
StartTimer(e)
HideLight(3)
keydown = 1
lightstate = 0
PlaySound(e,1)
-- Place spawns here --
elseif
g_KeyPressE == 1 and lightstate == 0 and keydown == 0 then
StartTimer(e)
ShowLight(3)
keydown = 1
lightstate = 1
PlaySound(e,1)
-- Place spawns here --
end
end
--end
end
end
-- Last end
Aftershock Quad Core AMD FM2+ 3.5 GHz 8GB Motherboard and Processor, A7700k apu, AMD HD7870 gfx card.
King Korg Synth, Alesis SR18 Drum Machine, Akai MPX8 sample player, Roland Fantom XA Synth, Axus Digital AXK2 Digital Drum Kit.