Over the past few days, I’ve been trying to make a script that up make a screen GUI open when you stepped on a part or went into a specific area, I want to make an area where a shop GUI would open if you were to walk inside that said area,
(The part that would detect the player stepping on it would be non-collide with a transparency of 1, I don’t know if this would or wouldn’t affect anything)
but I wasn’t able to reach this goal with my current knowledge of scripting and a thorough search online. How would I be able to achieve this?
local player = game:GetService("Players").LocalPlayer;
local gui = script.Parent;
workspace.ShopPart.Touched:Connect(function(hit)
if hit.Parent.Name == player.Name then
gui.Visible = true;
end;
end);
workspace.ShopPart.TouchedEnded:Connect(function(hit)
if hit.Parent.Name == player.Name then
gui.Visible = false;
end;
end);
Roblox’s TouchEnded isn’t the best and I would probably find a better way to register when they stop touching it
So in this case I think using a function called DistanceFromCharacter is best way of doing it.
It returns a number value
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local LocalPlayer = Players.LocalPlayer
local Shop = Workspace.Shop
local Distance = LocalPlayer:DistanceFromCharacter(Shop.Position)
print(Distance)
I gave you an example you have to do the rest if you have any questions feel free to ask me
When I want to make a pop up Ui, I just use Magnitude. It is more reliable than Touched/TouchedEnded. There are videos out there that go into more depth about Magnitude.
The way I tacked this issue is by using the DistanceFromCharacterAPI. Bind this to a RunService:RenderStepped() loop and you can simply check the returned number as a distance. For example…
runService.RenderStepped:Connect(function()
local distance = player:DistanceFromCharacter(Vector3.new(positionOfShopHitbox)
if (distance < activationDistance) then
--Show GUI
else
--Hide GUI
end
end)
positionOfShopHitbox would be the Vector3 position of the invisible part that would traditionally be the .Touched object.
activationDistance is a number that would be the minimum distance between the object and the player
's primary part.
I found a few, but as I said, they don’t work for me for some reason, I have the part as invisible and can-collide turned off if that affects anything.
So I had figured out why my old script didn’t work, NOTE: I’m saying this so other people may learn from my mistakes and not make the same mistakes that I have made. Anyways, the reason why my script didn’t work was because my script made the frame from startergui visible, instead of playergui. Why this didn’t work was because startergui only refreshes when you respawn, and playergui refreshes in real time.
(If you really want a script to make a pop-up shop gui, then heres a script for those who don’t know how to make one)
script.Parent.Touched:Connect(function(hit) --function that runs when the script's parent (in this case the part) when it is touched
if hit.Parent:FindFirstChild("Humanoid") then --checking if the thing that touched the part is a player
game:GetService("Players"):FindFirstChild(hit.Parent.Name).PlayerGui.ScreenGui.Frame.Visible = true --makes the frame inside of the screengui visible
end
end)