So im new to scripting and i want to do is, how can i make a screen gui to show up only when a player touches a certain part :>
First of all, do you mean touches as in clicked or as in their body touched a part?
If you want to call a function when the player touches a part with their body you should use part.Touched
If you want to click the part you have to insert a clickdetector into the part
You can used Touched events if you’re referring to a player touching a part. Here is a basic server script. Would also recommend adding a debounce in there. Mind the horrible formatting here, I’m on mobile.
local Part = script.Parent -- or wherever the part is located
local function onPlayerTouch(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then -- we know this is a player because it has a humanoid
local plrWhoTouched = game.Players:FindFirstChild(hit.Parent.Name)
plrWhoTouched.PlayerGui.ScreenGui.Frame.Visible = true
end
end
Part.Touched:Connect(onPlayerTouch)
In a lot of cases where an npc touches the part it will error, so try to confirm if the model that touched it is controlled by a player, other than that it’s good
thank u so much i will try this!
Let me know if it works for you. I wrote this under the assumption you have no NPCs in game as it wasn’t stated in the post, but if you do have NPCs keep in mind they have Humanoids as well so the script would error. You can check if it’s an NPC if you’d like as stated in the above reply.
local players = game:GetService("Players")
local part = script.Parent
local function onTouched(hitPart)
local hitModel = hitPart:FindFirstAncestorOfClass("Model")
if not hitModel then return end
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if not hitPlayer then return end
local hitPlayerGui = hitPlayer:FindFirstChildOfClass("PlayerGui")
if not hitPlayerGui then return end
local screenGui = hitPlayerGui:FindFirstChildOfClass("ScreenGui")
if not screenGui then return end
screenGui.Enabled = true
end
part.Touched:Connect(onTouched)
Relatively simple to achieve this.