How can i get player who clicked button but script need to be regular

i have problem. A proximity promt which opens ui but to close ui need to press button, proximity promt have a regular script but ui script to get player needs to be local but then i do ui script in local script its not working, but how can i access player who clicked in regular script(i can provide close script for ui, and proximity promt script to open, say what you need)

do you have a script for it so i can dive deeper

1 Like

proximity promt script this type of script is regular:

local Promt = script.Parent
Promt.Triggered:Connect(function(player)
	if player.PlayerGui.InGame.Enabled == false then
		player.PlayerGui.InGame.Enabled = true
	end
end)

close button script this needs to be regular too but if set to local working only 1 time (dont work, and i know player has not been announced anywhere):

local button = script.Parent
button.MouseButton1Click:Connect(function()
	if player.PlayerGui.InGame.Enabled == true then
		player.PlayerGui.InGame.Enabled = false
	end
end)


1 Like

@Theothegreat468 you here???

1 Like

Proximity Prompts | Documentation - Roblox Creator Hub

local ProximityPromptService = game:GetService("ProximityPromptService")

-- Detect when prompt is triggered
local function onPromptTriggered(promptObject, player)

end

-- Detect when prompt hold begins
local function onPromptHoldBegan(promptObject, player)

end

-- Detect when prompt hold ends
local function onPromptHoldEnded(promptObject, player)

end

-- Connect prompt events to handling functions
ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)
ProximityPromptService.PromptButtonHoldBegan:Connect(onPromptHoldBegan)
ProximityPromptService.PromptButtonHoldEnded:Connect(onPromptHoldEnded)
1 Like

you do realize you can just do .PromptTriggered on the client aswell?

1 Like

i did something like this but its still dont work and script is local and located inside proximitypromt

local ProximityPromptService = game:GetService("ProximityPromptService")

local function onPromptTriggered(promptObject, player)
	if script.Parent.Triggered then
		if player.PlayerGui.InGame.Enabled == false then
			player.PlayerGui.InGame.Enabled = true
		end
	end
end

ProximityPromptService.PromptTriggered:Connect(onPromptTriggered)
1 Like

it given errors in output and only work 1 time then dont work

1 Like

LocalScripts do not work when they’re descendants of Workspace

To run code on the client-side when the script’s in Workspace, you’ll need to use a server Script, but make sure to set it’s RunContext to Client (It’s set to Legacy by default, which makes it run on the server-side)

This appears to be quite the common problem, I think the docs should make this information more prominent

1 Like

thank you its is working but i see this error can you describe its bad or how i can fix this error:
Warning: The script with a non-legacy RunContext is parented to a container ‘StarterGui’, which will cause it to run multiple times. To avoid unintended behavior, consider setting the RunContext to ‘Legacy’, disabling the script, or moving it to a different location.

2 Likes

You’ll still need to use regular LocalScripts for GUIs which are in StarterGui

You’ll also need to use regular LocalScripts for:

  • Tools
  • Client-side scripts in StarterCharacterScripts
  • Scripts in StarterPlayerScripts
1 Like

The problem

I do not support the run context property. Scripts should not be allowed to execute in environments they were not designated for. There are certain containers under the data model that do not support the execution of any script, and StarterGui is one of them. The contents of StarterGui are cloned into folder under each client’s Player instance called “PlayerGui”, a container that does allow script execution. By changing the run context of a script in StarterGui, you’re enabling its original copy to execute in StarterGui and its clone to execute in the “PlayerGui” folder.

The solution

Instead, use a LocalScript under the GUI instance and reference the ProximityPrompt in workspace:

local ProximityPrompt = -- Path.to.ProximityPrompt
local InGame = script.Parent


local function enableGUI()
    InGame.Enabled = true
end


ProximityPrompt.Triggered:Connect(enableGUI)
2 Likes

Other game engines allow you to run client-side code no matter where they’re executing, RunContext was added as a means to allow Studio to do so as well

The reason why it shouldn’t be used in container services is because the scripts will attempt to run there as well, but using it in Workspace is perfectly safe


@OhioTeacup

I recommended that you use RunContext because I assumed that the script’s parent is the ProximityPrompt, and ProximityPrompts are usually descendants of Workspace

1 Like

ty man fixed error i jest needed local script not run context(bro why functions i dont prefer functions)

1 Like

You’re connecting a function to the event either way. Your approach uses a throwaway function instead. I prefer to use named functions as it improves consistency throughout the codebase while also providing a summary as to what the event listener does

1 Like

idk then i use local functions it gives me more errors and harder to me i prefer use connected function to events

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