Hello Robloxians,
I have encountered a scripting problem that has been stuck with me for a while now.
For some reason, my script is supposed to activate every time I touch the neon purple part.
As shown in the video it only worked once. When I closed the GUI. It did not work again. I did not use any destroy function.
What are my scripts and, where are they located?
There are 2 scripts. Scriptle, and Script. You can see their locations by the image below.
Scriptle:
local frame = script.Info
function showgui(part)
local player = game.Players:playerFromCharacter(part.Parent)
if part.Parent:findFirstChild("Humanoid") and player then
if not player.PlayerGui:findFirstChild("Info") then
local screengui = script.Info:clone()
screengui.Name = "Info"
screengui.Parent = player.PlayerGui
end
end
end
script.Parent.Touched:connect(showgui)
Script:
button = script.Parent
window = button.Parent
function onClicked(GUI)
window:remove()
end
script.Parent.MouseButton1Click:connect(onClicked)
Please help me in any way and form. I’m not a really good scripter.
Thank you for reading!
button = script.Parent
window = button.Parent
function onClicked(GUI)
window:remove()
end
script.Parent.MouseButton1Click:connect(onClicked)
So your problem here is that for one :remove I think the function is deprecated and instead of just cloning the UI every single time and waste memory, just make the UI not enabled and in this case your code will look like this.
--[[
Keep in mind to put the gui in startergui, disable it and name it Info.
]]--
local frame = script.Info
function showgui(part)
local player = game.Players:playerFromCharacter(part.Parent)
if part.Parent:findFirstChild("Humanoid") and player then
if not player.PlayerGui:findFirstChild("Info") then
local Info = player.PlayerGui:WaitForChild("Info")
Info.Enabled = true
end
end
end
script.Parent.Touched:connect(showgui)
Although :remove() is deprecated, it evidently still works and you should use :Destroy().
However, the issue is the fact that you’re deleting the frame and not the Info GUI, meaning that when the player touches the part again, it doesn’t make a new GUI as you already have an empty one called Info.
You should preferably use a RemoteEvent in ReplicatedStorage and then fire to the client if you want to do stuff such as when you touch a part and then a gui should be displayed.
I tried @DaffyDavinko 's solution. There is no output error.
I put the Info GUI into StarterGUI and the script stayed in the original part.
The problem is that the GUI itself is not willing to be Enabled.
In StarterGUI:
In Workspace:
In Scriptle:
--[[
Keep in mind to put the gui in startergui, disable it and name it Info.
]]--
local frame = game.StarterGui.Info
function showgui(part)
local player = game.Players:playerFromCharacter(part.Parent)
if part.Parent:findFirstChild("Humanoid") and player then
if not player.PlayerGui:findFirstChild("Info") then
local Info = player.PlayerGui:WaitForChild("Info")
Info.Enabled = true
end
end
end
script.Parent.Touched:connect(showgui)
In Script:
button = script.Parent
function onClicked(GUI)
button.Enabled = false
end
script.Parent.MouseButton1Click:connect(onClicked)
For god don’t use any Remotes for this… you can just do showing the gui by magnitude and I think it is way better.
Example of local script:
local PartTP = workspace.Part
local Player = game:GetService("Players").LocalPlayer
game:GetService("RunService").Heartbeat:Connect(function()
local mag = (PartTP.Position - player.Character.HumanoidRootPart.Position).Magnitude
if mag < 5 then --you can change the 5 to whatever distance
--show ui
else
--close ui
end
end)
@Wrvel lemme know if this is gonna work for you :).
You’re deleting the frame, not the GUI, simply fix the script with window:remove to
local button = script.Parent
local GUI = script:FindFirstAncestorWhichIsA("ScreenGui") --is faster than chaining a load of .Parent calls
button.MouseButton1Click:Connect(function()
GUI:Destroy()
end)
PS, its generally good practise to make your outermost variables local