Learning Luau (Part 2)

Hello Developers In these months I’ve been dedicating myself to learn Luau Leaving aside my Ex Extinct Community “WorldWide Developers” and I made a few scripts and I used GPT Chat to correct myself and this is the correction I got.

local workspace = game:GetService("Workspace")
local players = game:GetService("Players")
local controller1 = workspace.folder.Contoller:FindFirstChild("Part1")
local controller2 = workspace.folder.Contoller:FindFirstChild("Part2")
local controller3 = workspace.folder.Contoller:FindFirstChild("Part3")
local controller4 = workspace.folder.Contoller:FindFirstChild("Part4")
local luz1 = workspace.folder.lights.Light1.Light:FindFirstChild("PointLight")
local luz2 = workspace.folder.lights.Light2.Light:FindFirstChild("PointLight")
local luz3 = workspace.folder.lights.Light3.Light:FindFirstChild("PointLight")
local luz4 = workspace.folder.lights.Light4.Light:FindFirstChild("PointLight")

-- Import TextService
local TextService = game:GetService("TextService")
local TextChatService = TextService:GetChatForChannel("All")

-- Function to handle power outage
local function powerOutage(player)
	luz1.Enabled = false
	luz2.Enabled = false
	luz3.Enabled = false
	luz4.Enabled = false

	local playerName = player.DisplayName

	-- Filter the player name for display in the chat
	local filteredName = TextService:FilterStringAsync(playerName, player.UserId)

	-- Send a system message to the chat
	TextChatService:SendMessage(filteredName .. " triggered a power outage!")

	task.wait(60)

	luz1.Enabled = true
	luz2.Enabled = true
	luz3.Enabled = true
	luz4.Enabled = true
end

-- Function to handle touch events
local function onTouched(part)
	local player = players:GetPlayerFromCharacter(part.Parent)

	if player then
		powerOutage(player)
	end
end

-- Connect touch events for each cable
workspace["Torres De Alta Tension"].Cables.Part1.Touched:Connect(onTouched)
workspace["Torres De Alta Tension"].Cables.Part2.Touched:Connect(onTouched)
workspace["Torres De Alta Tension"].Cables.Part3.Touched:Connect(onTouched)
workspace["Torres De Alta Tension"].Cables.Part4.Touched:Connect(onTouched)



This code is based on the fact that if a player touches a high voltage cable, all the lights go out and a message is sent in the chat about the blackout and the player who caused the blackout with his display name and after 1 minute the lights are restored to normal and a message is sent again indicating that the outage has ended.

I want to add more functions like for example that the electric boards don’t work during the outage.

Fix The Mistake And Let Me Know!"

2 Likes

It would be better if you would put all of those una table and then iterate trough the table and disable them

1 Like

I am Requesting Correction

Please remember

local workspace = game:GetService("Workspace")
local players = game:GetService("Players")

local controllerTae = {
 controller1 = workspace.folder.Contoller:FindFirstChild("Part1"),
controller2 = workspace.folder.Contoller:FindFirstChild("Part2"),
controller3 = workspace.folder.Contoller:FindFirstChild("Part3"),
controller4 = workspace.folder.Contoller:FindFirstChild("Part4"),
}
local luzTable = {
luz1 = workspace.folder.lights.Light1.Light:FindFirstChild("PointLight"),
luz2 = workspace.folder.lights.Light2.Light:FindFirstChild("PointLight"),
luz3 = workspace.folder.lights.Light3.Light:FindFirstChild("PointLight"),
luz4 = workspace.folder.lights.Light4.Light:FindFirstChild("PointLight"),
}

-- Import TextService
local TextService = game:GetService("TextService")
local TextChatService = TextService:GetChatForChannel("All")

-- Function to handle power outage
local function powerOutage(player)
	for _, luzTable in pairs(luzTable) do
                luzTable.Enabled = false

	local playerName = player.DisplayName

	-- Filter the player name for display in the chat
	local filteredName = TextService:FilterStringAsync(playerName, player.UserId)

	-- Send a system message to the chat
	TextChatService:SendMessage(filteredName .. " triggered a power outage!")

	task.wait(60)

	for _, luzTable in pairs(luzTable) do
                luzTable.Enabled = true
end

-- Function to handle touch events
local function onTouched(part)
	local player = players:GetPlayerFromCharacter(part.Parent)

	if player then
		powerOutage(player)
	end
end

-- Connect touch events for each cable
workspace["Torres De Alta Tension"].Cables.Part1.Touched:Connect(onTouched)
workspace["Torres De Alta Tension"].Cables.Part2.Touched:Connect(onTouched)
workspace["Torres De Alta Tension"].Cables.Part3.Touched:Connect(onTouched)
workspace["Torres De Alta Tension"].Cables.Part4.Touched:Connect(onTouched)

GetChatForChannel is not a valid member of TextService “TextService”

That’s your script’s problem, I don’t know how that works, you made that

Others have the right to express their opinion and to correct it.

What did I say wrong?
I only said that it’s a problem I’m your Script, I simply copy-pasted it

1 Like

Here’s a script that should work:

Server side:

local controllers = game.workspace.folder.Controller:GetChildren()
local lights = {}

for _, light in ipairs(game.workspace.folder.lights:GetDescendants()) do
	if light:IsA("PointLight") then
		table.insert(lights, light)
	end
end

local SendMessage = game:GetService("ReplicatedStorage").SendMessage

local function powerOutage(player)	
	for _, light in ipairs(lights) do
		light.Enabled = false
	end

	SendMessage:FireAllClients({
		Text = player.DisplayName .. " triggered a power outage!";
        --you can add more properties to edit the text portrayed in chat.  
	})

	wait(60)

	for _, light in ipairs(lights) do
		light.Enabled = true
	end

SendMessage:FireAllClients({
		Text = "The power outage is over!";
        --you can add more properties to edit the text portrayed in chat.  
	})
end

local function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		powerOutage(player)
	end
end

for _, part in ipairs(game.workspace["Torres De Alta Tension"].Cables:GetChildren()) do
	if part:IsA("BasePart") then
		part.Touched:Connect(onTouched)
	end
end

Client Side:

local StarterGui = game:GetService("StarterGui")
local SendMessage = game:GetService("ReplicatedStorage").SendMessage

SendMessage.OnClientEvent:Connect(function(chatProperties)
	repeat
		wait() 
		local Success = pcall(function()
			StarterGui:SetCore("ChatMakeSystemMessage", chatProperties)
		end)
	until Success
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.