How can I make a button that purchases a model/group/part

I’m not the best coder but I’m trying to figure out how to make a button, like a tycoon button, where when a player touches it, it removes the in game currency, and spawns the object/model that only the player can see and no other player can. If anyone knows how I can accomplish this, please let me know, thank you so much.

local part = script.Parent -- your part here
local ServerStorage = game:GetService("ServerStorage")

part.Touched:Connect(function()
    if hit.Parent:findFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        -- Assuming you have a coin IntValue in leaderstats.Coins

        player:FindFirstChild("leaderstats").Coins.Value -= 10 -- put specified amount here
        -- ALSO assuming that there is a model in serverstorage

        ServerStorage:FindFirstChild("Your Item Here"):Clone().Parent = workspace
    end
end)

This should be a script with RunContext set to Client
(please let me know if it works)

It seems to have some issues with finding the hit.


My bad, I accidentally made a typo

its supposed to be part.Touched:Connect(function(hit)
You also need to put local Players = game:GetService("Players") for it to work

I fixed it, but is there a way to add a debounce, sorry I’m not the best with coding.

local part = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

part.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		local player = Players:GetPlayerFromCharacter(hit.Parent)

		player:FindFirstChild("leaderstats").Studs.Value -= 7500

		ServerStorage:FindFirstChild("StudFarm").StudFarm1:Clone().Parent = workspace
		part.Parent:Destroy()
	end
end)

hold on let me check on how to add debounce

I think I figured it out

local part = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local db = false

part.Touched:Connect(function(hit)
	if db == false then
		db = true
	if hit.Parent:findFirstChild("Humanoid") then
		local player = Players:GetPlayerFromCharacter(hit.Parent)

		player:FindFirstChild("leaderstats").Studs.Value -= 7500

		ServerStorage:FindFirstChild("StudFarm").StudFarm1:Clone().Parent = workspace
		part.Parent:Destroy()
		wait(0.1)
		db = false
	end
	end
end)

Yes, that works too for debounce

1 Like

Yup it works now, but one issue, even if the player doesn’t have enough studs, they can still purchase it.

You can just do this
Replace

player:FindFirstChild("leaderstats").Studs.Value -= 7500

with

local studs = player:FindFirstChild("leaderstats").Studs

if studs.Value >= 7500 then
    studs.Value -= 7500
end

Sorry, I forgot to account for that

1 Like

It works, but however now you cant even buy it and nor does it subtract studs. There isn’t an error or anything.

local part = script.Parent
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local db = false

part.Touched:Connect(function(hit)
	if db == false then
		db = true
	if hit.Parent:findFirstChild("Humanoid") then
		local player = Players:GetPlayerFromCharacter(hit.Parent)
			local studs = player:FindFirstChild("leaderstats").Studs

			if studs.Value >= 7500 then
				studs.Value -= 7500

		ServerStorage:FindFirstChild("StudFarm").StudFarm1:Clone().Parent = workspace
		part.Parent:Destroy()
		wait(0.1)
		db = false
	end
	end
	end
end)

I tested it, but it seems to work. What problem are you facing exactly?

(This is my script. I removed the part.Parent:Destroy() and temporarily put a black part as StudFarm )

I’ve noticed you tried to use part.Parent:Destroy(). If you wanted to destroy the buy part the script was under, it would be script.Parent:Destroy()

I have seem to figured it out, thank you so much for your help, I greatly appreciate it!

1 Like

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