How do I make a E to buy proximity prompt with leaderstats?

I want to make a part have a proximity prompt that gives me a tool when used. But I want it to be purchased with a leaderstat. For example, Buy Sword for 100 Points. The points would then be deducted from my leaderstats.

I just cannot get it to work. My points don’t get deducted, nor do I get the tool. There are no errors either.

local proximityPrompt = script.Parent
local purchase = game.ReplicatedStorage.Tools.ClassicSword

proximityPrompt.Triggered:Connect(function(player)
	local points = player.leaderstats:FindFirstChild("Points") 
	if points.Value >= 100 then
		points.Value = points.Value - 100
		local clone = purchase:Clone()
		clone.Parent = player.Backpack
	end
end)

I have asked people on discord servers, Youtube videos, and even ChatGPT but none of these have helped.

4 Likes

Use :WaitForChild for purchase, and make sure it’s server script.

2 Likes

I’ve been helping him with the game. The script still doesn’t work. It is a server script.

1 Like

try to print and see what is purchase and play and points they might return nil.

2 Likes

I did this:

local proximityPrompt = script.Parent
local purchase = game.ReplicatedStorage.Tools:WaitForChild("ClassicSword")
print(purchase)

proximityPrompt.Triggered:Connect(function(player)
	local points = player.leaderstats:FindFirstChild("Points")
	print(points)
	if points.Value >= 100 then
		print("Enough Points.")
		points.Value = points.Value - 100
		local clone = purchase:Clone()
		clone.Parent = player.Backpack
	else print("Not enough Points.") end
end)

and it prints out, ClassicSword, Points, and Not enough points

2 Likes

Get 100 points then buy the sword, you don’t have enough points. Your code works fine after all
For the Points print, print Points.Value instead to see how much points you have

I checked your script and it works successfully, most likely the problem is with the script that creates leaderstats and Points

alright, the output says I still dont have enough even when I have 100+

Are the points being added by a Script as well? There could be a mismatch between what the client believes and what the server knows if a LocalScript is handling the point provisions. You say you had more than 100 points, so I’m imagining those were awarded by the client.

Try changing the Triggered to PromptButtonHoldEnded

You mean if I gave myself the points in the explorer?

That’d be acceptable if you switched to the Server mode of Play Solo. I wouldn’t trust changing it in the default Client mode. That’s assuming you’re doing this while the game is running.

yeah I’m doing it when I enter the running game. How would I give myself points to actually test the game then?

Normal Scripts can change points, the command bar in Server mode can change points, and the F9 Server command bar (in an actual roblox game server you have permissions for) can change points. Make sure you’re targeting the right instance and not a template or something like that.

I’d like to provide some exact code and steps to try out but something came up and I won’t be online until a few hours later. I hope someone else can guide you.

2 Likes

that is all that I needed! thank you so much!

2 Likes

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