ProximityPrompt

I am trying to make it so that a GUI appears after the proximity prompt gets triggered, but it does not seem to work. The script is below:

local TS = game:GetService("TweenService")

script.Parent.ProximityPrompt.Triggered:Connect(function(hit)
	print("fired")
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player.PlayerGui:FindFirstChild("TeleportScreen") then else
			print("found")
			hit.Parent.HumanoidRootPart.Anchored = true
			game.SoundService.snor:Play()
			local clone = script.TeleportScreen:Clone()
			clone.Parent = player.PlayerGui
			clone.Enabled = true
			TS:Create(clone.TextLabel, TweenInfo.new(1), {BackgroundTransparency = 0, TextTransparency = 0}):Play()
		end
		else print ("failed")
	end
end)

The script prints “fired” and “failed”, and no other errors relating to this script. How can I fix this?

when a proximity prompt is triggered it has a Player argument inside it. Thus the hit you’re referring to is already a player and you don’t need to do Players:GetPlayerFromCharacter().

so pretty much

script.Parent.ProximityPrompt.Triggered:Connect(function(Plr)
	if not Plr.PlayerGui:FindFirstChild("TeleportScreen") thend
		-- do stuff :P
	end
end
2 Likes

Ah! The common Proximity Prompt bugs. Very tricky bugs and things that can frustrate a developer. Thankfully I can see the issue and it not a big one. It prints “Fired” and then “Failed” right after, correct? Well take a look at your function. It says script.Parent.ProximityPrompt.Triggered:Connect(function(hit) . Roblox doesn’t use “hit” for proximity prompts, as they use player. So if you change your script to script.Parent.ProximityPrompt.Triggered:Connect(function(player) and delete this line:
local player = game.Players:GetPlayerFromCharacter(hit.Parent) because Like @iATtaCk_Noobsi said, Player is already a player (not a character) you don’t need to use Players:GetPlayerFromCharacter(). Sorry if this doesn’t make sense. I will explain it a little bit better.

The reason it didn’t work

Proximity prompts (when using) use the Player argument, and not a body part (hit). So when using hit (which is odd but can work if you treat hit as a player) and esspecially hit.Parent, you’re referring to the player’s parent which is NOT a player and which is why you get “Fired” and “Failed”. So instead we change the hit to player (not 100% required but stronngly reccomended) and delete the line you had: local player = game.Players:GetPlayerFromCharacter(hit.Parent). It would have errored because hit.Parent isn’t even a character. So now this should work perfectly and flawlessly! Any more issues please feel free to share.

Was this made on the server side? because you cannot modify a player’s gui on the server side. Use RemoteEvents or Client scripts.
Also as other people already said, proximity prompts already return the player, you don’t have to do those checks to detect if it was a player because only a player can trigger them in the first place.

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