Proximity Prompt Visibility based on player item + position issue [Closed, Rewriting entire script]

Hello, I want assistance on a new Capture the flag script I am making.

Basically, when people get close to the flag, I want the flag to show 1 of 2 prompts depending on whether the user is carrying a flag model inside his Character.

The main script is a local script inside the playergui folder
Proximity prompts are located in Workspace.Game.Core.Flags.RedFlag.Handle

The script would detect when a player is near and enable/disable the prompt on the client side to run specific actions like move flag up and down the pole [global action] and clone flag model to persons character or would give points if depositing flag

This is what I have on the script right now, It does not seem to detect when player is nearby so it can enable/disable the prompts.

--[[									
	
								 Made By GameHounds Interactive
								  Do Not Redistribute
									Auguest 19th, 2024
									
							##### ___________________ #####
]]

local ProximityPromptService = game:GetService("ProximityPromptService")
local cfr = game.Workspace.Game.CORE.Flags.Red_Flag.Handle["Capture Flag"]
local cfb = game.Workspace.Game.CORE.Flags.Blue_Flag.Handle["Capture Flag"]
local dfr = game.Workspace.Game.CORE.Flags.Red_Flag.Handle["Deposit Flag"]
local dfb = game.Workspace.Game.CORE.Flags.Blue_Flag.Handle["Deposit Flag"]
local blueflag = game.Workspace.Game.CORE.Flags:FindFirstChild("Blue_Flag")
local redflag = game.Workspace.Game.CORE.Flags:FindFirstChild("Red_Flag")
local fm = script:FindFirstChild("Flag_Model")
local range = 20
local players = game:GetService("Players")

local gamepoints = game.ReplicatedStorage.Storage.Values.Red_Game_Points

-- cf.PromptShown
-- cf.PromptHidden

function capture_flag()
	-- Runs when flag captured, Puts flag model on back
	cfr.Triggered = true	
end

function deposit_flag()
	-- Runs when flag deposited to remove flag model from back and add game points
	dfr.Triggered = true
end

function captureprompt_started ()
	-- Make flag start lowering if capture prompt is engaged
	if cfr.PromptButtonHoldBegan == true and cfr.PromptButtonHoldBegan then
	
	end
end

function captureprompt_abandoned ()
	-- make flag reset to original position if capture prompt dis-engaged
	
	cfr.Enabled = false
	cfb.Enabled = false
	dfr.Enabled = false
	dfb.Enabled = false
end



while true do -- Fix code: Proxmity prompt not showing when close......
	wait(0.5)
	local PlayerTable = players:GetPlayers()

	for _,v in pairs (PlayerTable) do
		local Character = v.Character
		if not Character then return end

		local HRP = Character:WaitForChild("HumanoidRootPart")
		local Humanoid = Character:WaitForChild("Humanoid")

		local Distance = (HRP.CFrame.p - redflag.Handle.CFrame.p).Magnitude

		if Distance < range and Humanoid.Health > 0 then
			if Character:FindFirstChild("Flag") == true then
				dfr.Enabled = true
			else if Character:FindFirstChild("Flag") == false then
					cfr.Enabled = true
				end
			end
		else if Distance > range then
				dfr.Enabled = false
				cfr.Enabled = false
			end
		end

	end
end

Any help would be amazing!

4 Likes

The script looks like it should work. What does Distance print as?
If the Distance gets calculated correctly and the if-statement runs as intended, then what are the properties of the ProximityPrompt?

2 Likes

2 Likes

I added the distance print and did not print any object. Sorry I am a new scripter, but this screenshot should have tons of info.

I updated my main function block

 -- Fix code: Proxmity prompt not showing when close......
local Player =game.Players.LocalPlayer
local wmodel = Player.Character
local HRP = wmodel:WaitForChild("HumanoidRootPart")
local Humanoid = wmodel:WaitForChild("Humanoid")
local Distance = (HRP.CFrame.p - redflag.Handle.CFrame.p).Magnitude

while true do
	wait(0.5)
		if Distance < range and Humanoid.Health > 0 then
			print(Distance)
			if wmodel:FindFirstChild("Flag") == true then
				dfr.Enabled = true
			else if wmodel:FindFirstChild("Flag") == false then
					cfr.Enabled = true
					end
				end
			else if Distance > range then
				dfr.Enabled = false
				cfr.Enabled = false
		end
	end
end

If this script is a local script then you can get the local player easily with game.Players.LocalPlayer and get the character with player.Character or player.CharacterAdded:Wait(). You can also initialize these variables before the while-loop. Also try putting the print outside the if statement so that you can see what the Distance is printed as:

local player = game.Players.LocalPlayer

-- This either finds the character right away or waits until one is added
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")
local HRP = character:WaitForChild("HumanoidRootPart")

while true do -- Fix code: Proxmity prompt not showing when close......
	wait(0.5)

	local Distance = (HRP.CFrame.p - redflag.Handle.CFrame.p).Magnitude
	print(Distance)

	if Distance < range and Humanoid.Health > 0 then
		if wmodel:FindFirstChild("Flag") == true then
			dfr.Enabled = true
		elseif wmodel:FindFirstChild("Flag") == false then
			cfr.Enabled = true
		end
	elseif Distance > range then
		dfr.Enabled = false
		cfr.Enabled = false
	end
end

Also where is the red flag handle located in the world? Can the player realistically get within its range?

Edit: Ohh I just realised, you have to write the else if together like this elseif, when they’re separated the code won’t work as intended