Help on purchase system

My half finished purchase system is not working. Here is everything and I hope that you can help me…

LocalScript (child of Trails)

local Trails = script.Parent

local function ItemPurchase(ItemName, ItemPrize)
	local LocalPlayer = game.Players.LocalPlayer
	if LocalPlayer.Coins.Value >= ItemPrize then
		game.ReplicatedStorage.PurchaseItem:FireServer(ItemName, ItemPrize, LocalPlayer)
	end
end

Trails.RainbowTrail.Purchase.MouseButton1Click:Connect(ItemPurchase(Trails.RainbowTrail.Name, Trails.RainbowTrail.Prize.Value))
Trails.GreyTrail.Purchase.MouseButton1Click:Connect(ItemPurchase(Trails.GreyTrail.Name, Trails.GreyTrail.Prize.Value))

Script (ServerScriptService)

game.ReplicatedStorage.PurchaseItem.OnServerEvent:Connect(function(ItemName, ItemPrize, LocalPlayer)
	LocalPlayer.Coins.Value = LocalPlayer.Coins.Value - ItemPrize
	LocalPlayer.ShopItems:FindFirstChild(ItemName).Value = true
end)

PurchaseItem (RemoteEvent in ReplicatedStorage)

PlayerGui Players

the first parametr of Remote EVents is always the player who fired the event

so try this

game.ReplicatedStorage.PurchaseItem.OnServerEvent:Connect(function(plrname,ItemName, ItemPrize, LocalPlayer)
	LocalPlayer.Coins.Value = LocalPlayer.Coins.Value - ItemPrize
	LocalPlayer.ShopItems:FindFirstChild(ItemName).Value = true
end)
2 Likes

not related to the problem but have the server check if they can afford it and not the client so that exploiters can’t just get free items

1 Like

Or a probably better idea would be to have both check

1 Like

wouldn’t it make more sense to just do

game.ReplicatedStorage.PurchaseItem.OnServerEvent:Connect(function(plr,ItemName, ItemPrize)
	LocalPlayer.Coins.Value = LocalPlayer.Coins.Value - ItemPrize
	LocalPlayer.ShopItems:FindFirstChild(ItemName).Value = true
end)
1 Like

LocalScript
local Trails = script.Parent

local function ItemPurchase(ItemName, ItemPrize)
	local LocalPlayer = game.Players.LocalPlayer
	if LocalPlayer.Coins.Value >= ItemPrize then
		game.ReplicatedStorage.PurchaseItem:FireServer(LocalPlayer, ItemName, ItemPrize)
	end
end

Trails.RainbowTrail.Purchase.MouseButton1Click:Connect(ItemPurchase(Trails.RainbowTrail.Name, Trails.RainbowTrail.Prize.Value))
Trails.GreyTrail.Purchase.MouseButton1Click:Connect(ItemPurchase(Trails.GreyTrail.Name, Trails.GreyTrail.Prize.Value))
Script
game.ReplicatedStorage.PurchaseItem.OnServerEvent:Connect(function(LocalPlayer, ItemName, ItemPrize)
	if LocalPlayer.Coins.Value >= ItemPrize then
		LocalPlayer.Coins.Value = LocalPlayer.Coins.Value - ItemPrize
		LocalPlayer.ShopItems:FindFirstChild(ItemName).Value = true
	end
end)

maybe try this instead and with the other one too

Trails.RainbowTrail.Purchase.MouseButton1Click:Connect(function()
     ItemPurchase(Trails.RainbowTrail.Name, Trails.RainbowTrail.Prize.Value)
end)
1 Like

Yes, I think that helped a lot.


But “Coins” is an IntValue

Show your current scripts again

1 Like

These are the current scripts:

LocalScript
local Trails = script.Parent

local function ItemPurchase(ItemName, ItemPrize)
	local LocalPlayer = game.Players.LocalPlayer
	if LocalPlayer.Coins.Value >= ItemPrize then
		game.ReplicatedStorage.PurchaseItem:FireServer(LocalPlayer, ItemName, ItemPrize)
	end
end

Trails.RainbowTrail.Purchase.MouseButton1Click:Connect(function()
	ItemPurchase(Trails.RainbowTrail.Name, Trails.RainbowTrail.Prize.Value)
end)
Trails.GreyTrail.Purchase.MouseButton1Click:Connect(function()
	ItemPurchase(Trails.GreyTrail.Name, Trails.GreyTrail.Prize.Value)
end)
Script
game.ReplicatedStorage.PurchaseItem.OnServerEvent:Connect(function(LocalPlayer, ItemName, ItemPrize)
	if LocalPlayer.Coins.Value >= ItemPrize then
		LocalPlayer.Coins.Value = LocalPlayer.Coins.Value - ItemPrize
		LocalPlayer.ShopItems:FindFirstChild(ItemName).Value = true
	end
end)

try

if tonumber(LocalPlayer.Coins.Value) >= tonumber(ItemPrize) then

to make you’re comparing integer to integer

1 Like

Somehow ItemPrize is “GreyTrail”

maybe do

if type(tonumber(LocalPlayer.Coins.Value)) == "number" then
     if tonumber(LocalPlayer.Coins.Value) >= tonumber(ItemPrize) then
     --code
     end
end

to make sure when the script is running, that the type of Coins.Value is a number

1 Like

When firing don’t include local player as that is added in default because it’s in a localscript

game.ReplicatedStorage.PurchaseItem:FireServer(ItemName, ItemPrize)

Because that will give ItemPrize the value of the ItemName

1 Like

THANK YOU SO MUCH!
It works with this code:
:grinning: :grinning:

LocalScript
local Trails = script.Parent

local function ItemPurchase(ItemName, ItemPrize)
	local LocalPlayer = game.Players.LocalPlayer
	if LocalPlayer.Coins.Value >= ItemPrize then
		game.ReplicatedStorage.PurchaseItem:FireServer(ItemPrize, ItemName)
	end
end

Trails.RainbowTrail.Purchase.MouseButton1Click:Connect(function()
	ItemPurchase(Trails.RainbowTrail.Name, Trails.RainbowTrail.Prize.Value)
end)
Trails.GreyTrail.Purchase.MouseButton1Click:Connect(function()
	ItemPurchase(Trails.GreyTrail.Name, Trails.GreyTrail.Prize.Value)
end)
Script
game.ReplicatedStorage.PurchaseItem.OnServerEvent:Connect(function(LocalPlayer, ItemPrize, ItemName)
	if LocalPlayer.Coins.Value >= ItemPrize then
		LocalPlayer.Coins.Value = LocalPlayer.Coins.Value - ItemPrize
		LocalPlayer.ShopItems:FindFirstChild(ItemName).Value = true
	end
end)
1 Like