Buying part mechanism

I’m making an RPG game with my friend, and I’m trying to make a simple item buying mechanism.
Here is the code:

script.Parent.ClickDetector.MouseHoverEnter:Connect(function(player)
	local BuyGui = Instance.new("BillboardGui", script.Parent)
	local BuyText = Instance.new("TextLabel", BuyGui)
	BuyGui.AlwaysOnTop = true
	BuyText.BackgroundTransparency = 1
	BuyText.TextScaled = true
	BuyText.Text = "$100 Buy (Click)"
	BuyGui.MaxDistance = 20
	script.Parent.ClickDetector.MouseClick:Connect(function(player)
		player.leaderstats.Money.Value -= 100
		BuyText.Text = "Bought!"
		game.ServerStorage.Part.Parent = player.Backpack
	end)
	script.Parent.ClickDetector.MouseHoverLeave:Connect(function()
		BuyGui:Destroy()
	end)
end)

What happens when I test it:

robloxapp-20200928-1403388.wmv (2.2 MB)

2 Likes

Are you doing this in a localscript? You need to fire a remote to the server, to be able to a) confirm the player has enough cash & b) give the player the item. You can’t do that on the client.

1 Like

And use remote function instead of remote event (remote function allows you to return anything onto client if it fired from client)

1 Like

This is mostly a problem with the Tool, not the script.

Make sure the Tool has a “Handle” and that all the Tool parts are with Welds and are without Anchored :+1:t4:

no, i’m not doing it in a local script.

Yup, I figured that out before you posted this. Hopefully I remember next time.

1 Like

like this?

script.Parent.ClickDetector.MouseHoverEnter:Connect(function(player)
	local BuyGui = Instance.new("BillboardGui", script.Parent)
	local BuyText = Instance.new("TextLabel", BuyGui)
	local gpi = game:GetService("ReplicatedStorage").GivePlayerItem
	local BIHT = game:GetService("ReplicatedStorage").BuyItemHoverText
	BIHT:FireServer()
	BIHT.OnServerEvent:Connect(function()
		BuyGui.AlwaysOnTop = true
		BuyText.BackgroundTransparency = 1
		BuyText.TextScaled = true
		BuyText.Text = "$100 Buy (Click)"
		BuyGui.MaxDistance = 20
	end)
	script.Parent.ClickDetector.MouseClick:Connect(function(player)
		if player.leaderstats.Money.Value >= 100 then
			gpi:FireServer()
		else
			BuyText.Text = "Not enough cash."
			wait(3)
			BuyText.Text = "$100 Buy (Click)"
		end
	end)
	gpi.OnServerEvent:Connect(function()
		BuyText.Text = "Bought!"
		local toolClone = game:GetService("ServerStorage").Part:Clone()
		toolClone.Parent = player.Backpack
	end)
	script.Parent.ClickDetector.MouseHoverLeave:Connect(function()
		BuyGui:Destroy()
	end)
end)