This is probably a really simple thing however I’m not a scripter, I’ve searched through like 5 videos so far and all I want is a UI to become visible when a player is on a part, and not be visible when they step off. It’s really irritating how many approaches and videos I’ve tried yet none of them seem to work for such a simple thing.
The script I’m using is below, any help would be appreciated:
local part = script.Parent
local plr = game.Players.LocalPlayer
part.Touched:Connect(function(hit)
if plr:WaitForChild("Character") and plr:WaitForChild("Character"):IsDescendantOf(hit) then
local playerGui = plr:FindFirstChild("PlayerGui")
local ui = playerGui and playerGui:FindFirstChild("Frames") and playerGui.Frames:FindFirstChild("SwordsFrame")
if ui then
ui.Visible = true
end
end
end)
part.TouchEnded:Connect(function(hit)
if plr:WaitForChild("Character") and plr:WaitForChild("Character"):IsDescendantOf(hit) then
local playerGui = plr:FindFirstChild("PlayerGui")
local ui = playerGui and playerGui:FindFirstChild("Frames") and playerGui.Frames:FindFirstChild("SwordsFrame")
if ui then
ui.Visible = false
end
end
end)
The error I’m getting is 20:30:24.584 Workspace.SellCircle.Part.Script:6: attempt to index nil with ‘FindFirstChild’ - Server - Script:6.
Nevermind I already see that it is a server sided script,
So to explain you can’t simply get the player like you did it on the server.
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local playerGui = plr:FindFirstChild("PlayerGui")
local ui = playerGui and playerGui:FindFirstChild("Frames") and playerGui.Frames:FindFirstChild("SwordsFrame")
if ui then
ui.Visible = true
end
end
end)
part.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local playerGui = plr:FindFirstChild("PlayerGui")
local ui = playerGui and playerGui:FindFirstChild("Frames") and playerGui.Frames:FindFirstChild("SwordsFrame")
if ui then
ui.Visible = false
end
end
end)
I tpyed this on mobile so I’m not sure if its 100% correct. But we basically just checked if the parent of what we hit has a humanoid (all characters have humanoids) and if it does we know its a character and then we can get the player with a function from the Players service.
Not sure what your part is but maybe make the part something different if its just that little blue thing. So maybe an transparent part on top of it that would also be a bit bigger.
Because the flickering is from the touch ended event firing when I think your leg leaves the part.
Still had the same issue even when switching parts, I decided to resort to shortening the script so it just appears when the player touches the part and the player can manually close it with the close button. I think I prefer it that way anyway.
Anyways, it works fine at the start however if I close the UI and try step on it again it doesn’t appear. Any thoughts on why?
Well you probably only closed it on the client which means that it is theoretically still visible on the server. So turning it on on the server changes nothing.
What I recommend is making all the touched part stuff also on the client, or you could also do it in the server script. You could try it after the touched event with something like:
playerGui.YOUR CLOSE BUTTON.Activated:Connect(function()
Gui.Visible = false
end)
Thanks alot, that helped. I used your advice and changed it to something like:
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local playerGui = plr:FindFirstChild("PlayerGui")
local ui = playerGui and playerGui:FindFirstChild("Frames") and playerGui.Frames:FindFirstChild("SwordsFrame")
if ui then
ui.Visible = true
ui.ImageButton.MouseButton1Click:Connect(function()
ui.Visible = false
end)
end
end
end)