How to make Proximity prompt change an element in the player gui?

This is my code:

local screwnum = 0
local player = game:GetService("Players")
local gui = player:WaitForChild("PlayerGui")
local screws = gui.ScrewUI
screws.ScrewNUM.Text = screwnum

--script.Parent.ProximityPrompt.PromptButtonHoldEnded:Connect(function()
--	print(screwnum)
--end)

while true do
	wait(1)
	print(screws.ScrewNum.Text)
end

this is what my player looks like in the workspace when i play the game:

image_2024-06-24_175306752

why is my code not working?

1 Like

also i know the part of my code that would change it is commented it, but it should be at least able to print screwnum, but it isnt

1 Like

Any output? And it is supposed to be print(screws.ScrewNUM.Text) (capitalized NUM).

Edit: You get the LocalPlayer via game:GetService("Players").LocalPlayer. There is also a good possibility ScrewUI is not replicated before the script fires, assuming your script is in the StarterPlayerScripts.

2 Likes

i edited my code keeping everything you said in mind and defined screws inside of the loop (which i know is most likely not good practice, but this is just for testing) and it still is not outputting anything

local screwnum = 0
local player = game:GetService("Players").LocalPlayer
local gui = player:WaitForChild("PlayerGui")

--script.Parent.ProximityPrompt.PromptButtonHoldEnded:Connect(function()
--	print(screwnum)
--end)

while true do
	wait(1)
	local screws = gui.ScrewUI
	screws.ScrewNUM.Text = screwnum
	print(screws.ScrewNUM.Text)
end

also, the localscript is under the object i have the proximityprompt in, not localplayerscripts, i was trying to avoid doing that but is that the only way this would be possible?

1 Like

What type of script are you actually using and where is it located?

1 Like

(just edited the original message to say the info you want, lol)

1 Like

That is the problem. LocalScripts only runs on descendants of specific containers - it will not run under workspace. Instead, you can either:

  1. Change it into a server-sided Script so it will run, and fire a RemoteEvent that will update the ScreenGui in another LocalScript inside the gui itself.
  2. Move your LocalScript inside your actual ScreenGui and reference the ProximityPrompt.
  3. Change your script into a Script and set the RunContext to Client, essentially running client-sided code that runs in the workspace.

It is recommended to use method 1 over 2 since the ProximityPrompt might not always exist on the client because it might not have loaded yet or is outside the range of streaming. This guarantees the server keeping track of the ProximityPrompt at all times and sending the client the information when needed.

Method 3 might also not be the best method since I am not 100% on how the LocalScript will react if it loads first before the actual Part containing the ProximityPrompt (if assuming it loads that way).

1 Like

i was doing it this way cus idk how to use remoteevents correctly ;-;

1 Like

here try this you made some miss Type some names and also in Proximity prompt the PromptButtonHoldEnded is not working so I change it to Triggered. also I change your ways of declaring the paths for the object because your is direct and sometimes can cause and error.

local screwnum = 0
local player = game:GetService("Players")
local gui = player.LocalPlayer:WaitForChild("PlayerGui")
local screws = gui:FindFirstChild("ScrewUI")


game.Workspace:WaitForChild("Folder"):FindFirstChild("Part"):FindFirstChild("ProximityPrompt").Triggered:Connect(function()
	screws:FindFirstChild("ScrewNUM").Text = screwnum
	screwnum += 1
	print(screwnum)
end)

while true do
	wait(1)
	print(screws:FindFirstChild("ScrewNUM").Text)
end

after a little bit of work your code worked! i just had to change the names of the children the code was looking for (obviously) thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.