I have been trying to make a ScreenGui appear when a Part is touched, however it does not seem to appear, and I think there may be an error in the code I am using. I appreciate anyone who can help me resolve this. Thanks.
local Model = game.Workspace.Part function destroy() game.Workspace.Part:Destroy() end script.Parent.Touched:connect(destroy) function move() local touched Model.Touched:connect(function(hit) if hit and hit.Parent:FindFirstChild("Humanoid") then touched = true end end) while not touched do Model.Position = Vector3.new(math.random(),math.random(),math.random())*10 wait(0.5) end end game.Players.PlayerAdded:connect(move) function screen(Part) local Gui = game:GetService("ServerStorage").ScreenGui local Player = game.Players:GetPlayerFromCharacter(Part.Parent) if Player and not Player.PlayerGui:FindFirstChild(Gui.Name) then Gui:Clone().Parent = Player.PlayerGui end end script.Parent.Touched:connect(screen)
The Touched
event takes the Part it hit as its only parameter. You forgot to include that in the screen()
function.
Also the part might have been destroyed before the rest of the script can run.
local Model = game.Workspace.Part function move() local touched Model.Touched:connect(function(hit) if hit and hit.Parent:FindFirstChild("Humanoid") then touched = true end end) while not touched do Model.Position = Vector3.new(math.random(),math.random(),math.random())*10 wait(0.5) end end game.Players.PlayerAdded:connect(move) function screen(Part) local Gui = game:GetService("ServerStorage").ScreenGui local Player = game.Players:GetPlayerFromCharacter(Part.Parent) if Player and not Player.PlayerGui:FindFirstChild(Gui.Name) then Gui:Clone().Parent = Player.PlayerGui end game.Workspace.Part:Destroy() end script.Parent.Touched:connect(screen)