Proximity prompt issue

Hello developers,
I am very new to coding and have been wanting to make a work script for my game. I made a proximity prompt so when you finish holding it down, it gives you the money, but I cam across an issue: When I would just click the key for proximity prompt, it would grant the money even though I hadn’t finished filling the circle yet.
I am not asking for scripts but can someone show me where to put the money giver part of the script so you have to finish the whole prompt before it gives you the money, my script is below.

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)
	local leaderstats = player.leaderstats
	local moneyStat = leaderstats and leaderstats:FindFirstChild("Cash")
	if moneyStat then
		moneyStat.Value = moneyStat.Value + 10
	end
end

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

Regards, oPenary.

2 Likes

you didn’t specify the player or call the player from the function.

1 Like

Can you explain this, sorry if this is annoying, I am very new.

1 Like

Alright, Np; So You Made 3 Functions: onPromptTriggered, onPromptHoldBegan, onPromptHoldEnded, So You’ve Putted 2 Parameters Called: promptObject and player You’ve To Call Them.

Here’s The Solve:


game.Players.PlayerAdded:Connect(function(Player)
	ProximityPromptService.PromptTriggered:Connect(function(promptObject)
		-- Here's What You Want to do after Triggering The Prompt.
	end)
	ProximityPromptService.PromptButtonHoldBegan:Connect(function(promptObject)
		-- Here's What You Want to do after Holding The Prompt.
	end)
	ProximityPromptService.PromptButtonHoldEnded:Connect(function(promptObject)
		local leaderstats = Player.leaderstats
		local moneyStat = leaderstats and leaderstats:FindFirstChild("Cash")
		if moneyStat then
			moneyStat.Value = moneyStat.Value + 10
		end
	end)
end)

Thanks,
Mezo.
2 Likes

It doesn’t seem to work, I have tried changing it but still no success.

Maybe I have set it up wrong? Can you explain??

Hi there! I would recommend checking out the Proximity Prompts article/tutorial. Have a read through, and try and see if it helps. But if it doesn’t, just come back to this topic for some help!

Hey Mashmello, I originally wanted a proximity prompt in my game, that is where the orignal script came from, I could get it to work.

Alright, would you be able to explain your error/issue? As I’m not fully sure what your error here is.

Ok, the first error I had with the code was that it would give the money when you clicked e instead of holding it down for the full 30 secs, then the code Mezo had which I then editted after testing, didn’t give the money at all

game.Players.PlayerAdded:Connect(function(Player)
	ProximityPromptService.PromptTriggered:Connect(function(promptObject)
		-- Here's What You Want to do after Triggering The Prompt.
	end)
	ProximityPromptService.PromptButtonHoldBegan:Connect(function(promptObject)
		-- Here's What You Want to do after Holding The Prompt.
	end)
	ProximityPromptService.PromptButtonHoldEnded:Connect(function(promptObject)
		local gold = Instance.new("IntValue")
		local leaderstats = player.leaderstats
		local goldStat = leaderstats and leaderstats:FindFirstChild("Cash")
		if goldStat then
			goldStat.Value = goldStat.Value + 10
		end
	end)
end)

The code at the moment.

Already got your first problem. With ProximityPrompts you don’t need to use the game.Players.PlayerAdded:Connect(function(player) function, remove that.

Another one, under the last function (PromptButtonHoldEnded). You can delete the first line after the function the (local gold = Instance.new(“IntValue”)) as it appears to be irrevalant.

Try that now, with those edits/changes you should be good to go. If not, I may actually know why.

It still doesn’t work, the leaderstat script is in a diferent script, I think thats supposed to update it, it did before. Being new to scripting, this has really stumped me.

I had a feeling, there was something missing/wrong about this script. I just double checked the ProximityPrompt Article. You missed 3 vital lines of codes.

Post these lines of code, at the end of your script (Where those ProximityPrompt functions are) Make sure that these lines of code, are ALWAYS at the end of the script. Or it could risk breaking your script, as it may get stuck on it.

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

Simple, i just dont use that service, the Proximty prompt has its own event! Triggered, I think that only opens when you start the Trigger and just start input, when it begins!!! try putting the script in the Proximity Prompt and saying

script.Parent.Triggered:Connect(function()
local leaderstats = player.leaderstats
	local moneyStat = leaderstats and leaderstats:FindFirstChild("Cash")
	if moneyStat then
		moneyStat.Value = moneyStat.Value + 10
	end
end)

hope this helps~!

It still don’t work :frowning:, There are blue lines under the first three functions, here is the code I am still stuck.

	ProximityPromptService.PromptTriggered:Connect(function(promptObject)
		-- Here's What You Want to do after Triggering The Prompt.
	end)
	ProximityPromptService.PromptButtonHoldBegan:Connect(function(promptObject)
		-- Here's What You Want to do after Holding The Prompt.
	end)
	ProximityPromptService.PromptButtonHoldEnded:Connect(function(promptObject)
		local leaderstats = player.leaderstats
		local goldStat = leaderstats and leaderstats:FindFirstChild("Cash")
		if goldStat then
			goldStat.Value = goldStat.Value + 10
		end
	end)
end)

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

Since I am new, can you elaborate or explain this to me, im not too sure I understand.

hmm, so basically:
when i said script i mean the script the code is being executed inside!
Parent is the like… Instance that is above it, I hope you know what it means :> if you don’t you will find out soon don’t worry! Anyways
ProximityPromptService is a Service I never use ;-;, i don’t even understand it!
But when i used it, It usually never does an action like giving money once you fully completed the circle in the proximity prompt! Try putting a script inside the ProximiptyPrompt your using and slowly type:

script.Parent.Triggered:Connect(function()
end)
--confused? this is the exact same as:
local function onPromptTriggered()
-- your code can go here
end

script.Parent.Triggered:Connect(onPromptTriggered) -- this will only do it once the circle is completed :D

Good luck learning scripting if you are interested :slight_smile:

Thanks for all the help everyone, but I got it to work.

local ProximityPromptService = game:GetService("ProximityPromptService")

-- Detect when prompt is triggered
local function onPromptTriggered(promptObject, player)
	local leaderstats = player.leaderstats
	local goldStat = leaderstats and leaderstats:FindFirstChild("Cash")
	if goldStat then
		goldStat.Value = goldStat.Value + 10
	end
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