In this game, signs will display text in languages that do not exist in real life. Therefore, I have created a script that displays the translated text on the player’s screen while they are mousing over the sign’s BasePart.
- I need to be able to stop the whole thread from running, because the arguments will change within a gameplay session.
Since I’m pretty new to programming overall, I do not think this is a good script whatsoever. In making this post I’m looking for any obvious improvements or holes in the logic of this script. If I’m breaking any coding norms I’d like to know about that too.
Since I only want a review of this module in specific and it’s short, I’ve included it here as text instead of an rbxl file.
local SignModule = {["Local"]={}}
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
--Local Script Variables
local LocalPlayer = PlayerService.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local WaitForMouseOverThread
function SignModule.Local.WaitForMouseOver(knownLang, recogLanguages)
local showSign = function()--function that waits time and then shows the gui
wait(0.4)
LocalPlayer.PlayerGui.SignGui.Enabled = true
end
local bindGUI = function()--function that binds the gui to the player mouse
RunService.RenderStepped:Connect(function()
LocalPlayer.PlayerGui.SignGui.TextLabel.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)
end)
end
for i, signPart in game.Workspace.Signs:GetChildren() do--indexes all signParts
local co
local bind
--these two variables are needed in the coming connection as coroutines
signPart.ClickDetector.MouseHoverEnter:Connect(function()--runs once mouse has entered a signPart
co = coroutine.create(showSign)
bind = coroutine.create(bindGUI)
if knownLang then
for i, v in knownLang do--checks passed table(list) of known languages
if v == signPart:GetAttribute("Language") then--if its the language set that text!
LocalPlayer.PlayerGui.SignGui.TextLabel.Text = signPart:GetAttribute("TranslatedText")
end
end
end
--for both of these itd be cool to kill the iterator once v is verified
if recogLanguages then
for i, v in recogLanguages do
if v == signPart:GetAttribute("Language") then--if its the language set that lame text!
LocalPlayer.PlayerGui.SignGui.TextLabel.Text = signPart:GetAttribute("RecognizedText")
end
end
end
LocalPlayer.PlayerGui.SignGui.TextLabel.Text = signPart:GetAttribute("TranslatedText")
coroutine.resume(co)--playing those coroutines i made at the beginning of the function
coroutine.resume(bind)
end)
signPart.ClickDetector.MouseHoverLeave:Connect(function()--runs once mouse has left a SignPart
LocalPlayer.PlayerGui.SignGui.Enabled = false
coroutine.close(co)--KILL THEM!!!
coroutine.close(bind)
end)
end
end
function SignModule.InitializeWaitForMouseOver(knownLang, recogLang)
WaitForMouseOverThread = coroutine.create(SignModule.Local.WaitForMouseOver)
coroutine.resume(WaitForMouseOverThread, knownLang, recogLang)
end
function SignModule.KilLWaitforMouseOver()
coroutine.close(WaitForMouseOverThread)
end
return SignModule