What's stopping this script from working?

I wish to make it so that a proximityprompt is only shown once a tool by the name of “Tray” is equipped. However, so far it isn’t doing what it’s supposed to, as despite the “tray” tool being held, the prompt doesn’t appear. Below you’ll find the problematic code, and the hierarchy of where the proximityprompt is shown.

Localscript

local ProximityPrompt = game:GetService("ProximityPromptService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local function ToolEquipped(prompt)
	local tool = player.Character:FindFirstChildWhichIsA("Tool")
	if tool ~= nil then
		prompt.Enabled = true
	end
end

local function CheckPrompt(prompt)
	if prompt.Name == "Tray" then
		if player.Character:FindFirstChildWhichIsA("Tool") ~= nil then
			prompt.Enabled = true
		else
			prompt.Enabled = false
			player.Character.ChildAdded:Connect(function()
				ToolEquipped(prompt)
			end)
		end
	end
end

ProximityPrompt.PromptShown:Connect(CheckPrompt)

Hierarchy
image

1 Like

It’s in workspace, just to let you know.

1 Like

uh correct me if i’m wrong but i think you should be doing prompt.Parent.Name

1 Like

I have attempted this, but to no avail.

1 Like

It’s because it’s a LocalScript, which does not run in the Workspace. A LocalScript will run in:

  • ReplicatedFirst
  • StarterGui
  • StarterPlayerScripts (in a Player)
  • StarterCharacterScripts (in a Character)
  • StarterPack (in a Tool)

Move the script to one of these places or use a normal Script with the RunContext set to Enum.RunContext.Client.

2 Likes

How would I go about adding the Enum.RunContext.Client? Where would it go?

2 Likes

If you select the script, in the Properties tab select the RunContext dropdown and select Client.




Or, if you want the method that takes more work (not necessary but I’ll put it here for future reference if anyone needs it):
Select the script and run this in the command bar:

game:GetService("Selection"):Get()[1].RunContext = Enum.RunContext.Client

Note this must originally be a server script (not LocalScript)

2 Likes

Oddly enough, even if RunContext is selected to client, it still isn’t doing what I want it to

image

1 Like

The prompt should be showing upon the tray being selected, but it isn’t showing up unfortunately

2 Likes

Just to make sure it’s not a problem with your code, move this to a LocalScript and put it in one of the locations I said (likely StarterPlayerScripts or StarterCharacterScripts in this case).

I’ve never used the RunContext feature, it’s likely I got it wrong :sweat_smile:

1 Like

The script does show as a localscript, so I doubt you did it wrong…
image

1 Like

It seems that ProximityPrompt are no longer functional inside of the ‘model’ alone, try moving it to a BasePart in the model or inside of an attachment in `workspace.Terrain``

As 12345koip mentioned, if you want localscripts to run in workspace you have to change the RunContext Property in the properties tab of a ‘server-script’ to ‘Client’.

edit 2: make sure LineOfSight property of the ProximityPrompt is disabled.

stove.rbxl (56.9 KB)

Hm… It still does show up, but, it shows up regardless of the script that’s supposed to only make it show up whenever the tray tool is equipped…

image

The problem is the logic of the code.

ProximityPrompt.PromptShown:Connect(CheckPrompt)

Will only trigger if a prompt is shown, so using it to enable/disable it self isn’t the right way to do it.

What you have to do is to enable and disable the prompt based on whether the tool is in the character or not.

I linked a rbxl file to the topic above, which shows another approach to achieve the same result. You will have to change line 5 to make it work, i forgot to change it back when I uploaded the file.

Thank you! It all works as intended.

How would I go about fixing this warning? It seems to spam my output, and I’ve tried adding wait statements to the script, but that didn’t fix it.
image

For context, I have a team switch functionality, and so if unresolved, it would cause some problems.

That was a bad example to clean up connections, you can either look up how to clean up connections or remove that line from the script. I don’t think it will hurt as long as you’re willing to learn about them later on. Just have fun for now.

edit: i just realized that you marked the post as a solution, thank you.

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