Attempt to make Purchase RemoteFunction to my Shop

  1. What do you want to achieve?
    I Want to make a Shop Manager for my Shop Gui.

  2. What is the issue?
    it’s doesn’t work properly and I Can’t understand what’s wrong with my script…

  3. What solutions have you tried so far?
    I was trying to search about that, but i found nothing

There the code is:

local Players = game:GetService("Players");
local RPS = game:GetService("ReplicatedStorage");
local Purchase = RPS:WaitForChild("Remotes").Purchase;

local function GetType(Type)
	if Type == "Emotes" then
		return require(RPS:WaitForChild("Modules").EmotesModule);
	elseif Type == "Weapons" then
		return require(RPS:WaitForChild("Modules").ToolsModule);
	end;
end;

local function CreateNewVal(Name, Value, Parent)
	local newVal = Instance.new("StringValue");
	newVal.Name = Name;
	newVal.Value = Value;
	newVal.Parent = Parent;
end;

Purchase.OnServerInvoke = function(Player, Type: string, Value: string)
	local dataBase = Player[Player.UserId];
	local Owned = dataBase["Owned"];
	local Equipped = dataBase["Equipped"];
	
	local leaderstats = Player["leaderstats"];
	local module = GetType(Type);
	
	if module[Value] then
		for _, value in pairs(Owned[Type]:GetChildren()) do
			if value.Value == Value and Equipped[Type].Value ~= Value then
				Equipped[Type].Value = Value;
				
				return "Equip";
			end;
			
			if value.Value ~= Value then
				local currency;
				
				if not module[Value].Currency then
					currency = 0;
				else
					currency = module[Value].Currency;
				end;
					
				if leaderstats["Снюсы"].Value >= tonumber(currency) then
					leaderstats["Снюсы"].Value -= tonumber(currency);
					
					Equipped[Type].Value = Value;
					CreateNewVal("Owned", Value, Owned[Type]);
				end
				break;
			end;
		end;
	end;
end;

Client Code

----Services----

local Players = game:GetService("Players");

local RS = game:GetService("RunService");

local RPS = game:GetService("ReplicatedStorage");

local GS = game:GetService("GuiService");

local SG = game:GetService("StarterGui");

local UIS = game:GetService("UserInputService");

----Modules----

local ToolsModule = require(RPS:WaitForChild("Modules").ToolsModule);

local EmotesModule = require(RPS:WaitForChild("Modules").EmotesModule);

----Variables----

local camera = Instance.new("Camera");

local Template = script.Parent.Shop.PreviewSection.Template;

local ViewPort = Template.Objection;

local CurrentCamera = workspace.CurrentCamera;

local ShopFrame, Preview, Selection, MenuSection = script.Parent.Shop, script.Parent.Shop.PreviewSection, script.Parent.Shop.SelectedSection, script.Parent.Shop.MenuSection;

local Remotes = RPS:WaitForChild("Remotes");

local Player = Players.LocalPlayer;

local EmoteDummy = RPS:WaitForChild("Showcase");

local SelectionPreview = Selection.SelectionPreview;

local Menu = script.Parent.Menu;

local Purchase = Remotes.Purchase;

local EmotePlay = Remotes.PlayEmote;

camera.CameraType = Enum.CameraType.Scriptable;

camera.CFrame = CFrame.new();

Template.Objection.CurrentCamera = camera;

ShopFrame.Draggable = true;

local CamCF = camera.CFrame

local CamLV = CamCF.lookVector;

local CamPos = CamCF.p

local Currency = SelectionPreview.Currency;

local SelectionDescription = SelectionPreview.Description;

local Handler = Preview.Handler;

local CF = CFrame.new;

local SelectionString = "Selection";

local EmoteString = "Emote"

local SelectionTitle = SelectionPreview.Title;

local SelectionShow = SelectionPreview.Showcase;

local SelectionWM = SelectionShow.WorldModel;

local SelectionSelect = Selection.Select;

local SPpurchase = SelectionPreview.Purchase;

local SPCurrency = SelectionPreview.Currency;

local DataBase = Player[Player.UserId];

local Equipped = DataBase["Equipped"];

local Owned = DataBase["Owned"];

----Functions/Connections----

UIS.InputBegan:Connect(function(Input)

if not Player.Character:GetAttribute(EmoteString) and Input.KeyCode == Enum.KeyCode.G then

EmotePlay:FireServer();

end;

end);

local function ClearViewportFrame(viewport)

for _, obj in pairs(viewport) do

if obj:IsA("BasePart") or obj:IsA("Model") then

obj:Destroy();

end;

end;

end;

local function ClearFrame(frame)

for _, obj in pairs(frame) do

if obj:IsA("Frame") then

obj:Destroy();

end;

end;

SelectionSelect.Visible = true;

SelectionPreview.Visible = false;

Selection.BackgroundTransparency = 0.5;

SelectionDescription.Visible = true;

end;

local function GetType(value: string)

if EmotesModule[value] then

return "Emote";

end;

if ToolsModule[value] then

return "Tool";

end;

end;

local function EmoteMenu()

ClearFrame(Handler:GetChildren());

for _, emote in pairs(EmotesModule) do

local emoteAnim = emote.Animation;

local emoteStart = emote.EmoteStart;

local emoteCurrency = emote.Currency;

local emoteName = emote.Name;

local newDummy = EmoteDummy:Clone();

local newTemplate = Template:Clone();

local newCurrency = newTemplate.Currency;

local newPreview = newTemplate.Preview;

local newObject = newTemplate.Objection;

newTemplate.Name = emote.Name;

newTemplate.Visible = true;

newTemplate.Parent = Preview.Handler;

if emoteCurrency then

newCurrency.Text = emoteCurrency .. " $";

end;

newPreview.MouseButton1Click:Connect(function()

Selection:SetAttribute(SelectionString, emoteName);

end);

newDummy.Parent = newObject["WorldModel"];

newDummy:SetPrimaryPartCFrame(CF(CamLV * 6.5, CamPos));

if emoteStart then

emoteStart(newDummy);

end;

local dummyHum = newDummy.Humanoid;

local humAnimator = dummyHum.Animator;

local anim = humAnimator:LoadAnimation(emoteAnim);

anim.Looped = true;

anim:Play();

end;

end;

local function ToolMenu()

ClearFrame(Handler:GetChildren());

for _, tool in pairs(ToolsModule) do

local toolTool = tool.Tool;

local toolName = tool.Name;

local currency = tool.Currency;

local newTemplate = Template:Clone();

local newItem: BasePart = toolTool:Clone();

local newCurrency = newTemplate.Currency;

local newPreview = newTemplate.Preview;

local newObject = newTemplate.Objection;

newTemplate.Name = toolName;

newTemplate.Parent = Preview.Handler;

newTemplate.Visible = true;

if currency then

newCurrency.Text = currency .. " $";

end;

newPreview.MouseButton1Click:Connect(function()

Selection:SetAttribute(SelectionString, toolName);

end);

newItem.CFrame = CF(CamLV * 3, CamPos);

newItem.Parent = newObject;

task.spawn(function()

while task.wait(0.001) and newItem do

newItem.CFrame *= CFrame.Angles(0, 0.05, 0);

end

end);

end;

end;

MenuSection.EmoteSection.MouseButton1Click:Connect(function()

Preview:SetAttribute("Menu", EmoteString);

end);

MenuSection.ToolSection.MouseButton1Click:Connect(function()

Preview:SetAttribute("Menu", "Tool");

end);

MenuSection.ToySection.MouseButton1Click:Connect(function()

Preview:SetAttribute("Menu", "Toy");

end);

----Setting Properties/Attributes----

task.spawn(function()

repeat task.wait() until ShopFrame.Visible;

ToolMenu();

end);

Preview.AttributeChanged:Connect(function(attribute)

if attribute == "Menu" then

local selection = Preview:GetAttribute(attribute);

if selection == "Emote" then

EmoteMenu();

elseif selection == "Tool" then

ToolMenu();

end;

end;

end);

Selection.AttributeChanged:Connect(function(attribute)

if attribute == "Selection" then

--Emote--

if GetType(Selection:GetAttribute("Selection")) == "Emote" then

Selection.BackgroundTransparency = 1;

SelectionSelect.Visible = false;

SelectionPreview.Visible = true;

local Emote = EmotesModule[Selection:GetAttribute(SelectionString)];

local emoteName = Emote.Name;

local emoteStart = Emote.EmoteStart;

local emoteAnim = Emote.Animation;

local emoteCurrency = Emote.Currency;

local emoteDescription = Emote.Description;

local debounce = false;

local newDummy = EmoteDummy:Clone();

ClearViewportFrame(SelectionWM:GetChildren());

SelectionTitle.Text = emoteName;

newDummy:SetPrimaryPartCFrame(CF(CamLV * 6.5, CamPos));

newDummy.Parent = SelectionWM;

if emoteStart then

emoteStart(newDummy);

end;

local dummyHum = newDummy.Humanoid;

local humAnimator = dummyHum.Animator;

local anim = humAnimator:LoadAnimation(emoteAnim);

anim.Looped = true;

anim:Play();

SPpurchase.MouseButton1Click:Connect(function()

if SPpurchase.Text ~= "Экипировано" and not debounce then

Purchase:InvokeServer("Emotes", emoteName);

delay(2, function()

debounce = true;

end);

end;

end);

for _, value in pairs(Owned["Emotes"]:GetChildren()) do

if Equipped["Emotes"].Value == emoteName then

SPpurchase.Text = "Экипировано";

elseif value.Value == emoteName and Equipped["Emotes"].Value ~= emoteName then

SPpurchase.Text = "Экипировать";

else

SPpurchase.Text = "Купить";

end;

end;

if emoteCurrency then

SPCurrency.Text = Emote.Currency .. " $";

SPpurchase.Text = "Купить";

else

SPCurrency.Text = "";

SPpurchase.Text = "Экипировать";

end;

if emoteDescription then

SelectionDescription.Text = emoteDescription;

end;

end;

--Tool--

if GetType(Selection:GetAttribute(SelectionString)) == "Tool" then

Selection.BackgroundTransparency = 1;

SelectionSelect.Visible = false;

SelectionPreview.Visible = true;

local Tool = ToolsModule[Selection:GetAttribute("Selection")];

local toolName = Tool.Name;

local toolCurrency = Tool.Currency;

local toolDesc = Tool.Description;

local debounce = false;

local newItem = Tool.Tool:Clone();

ClearViewportFrame(SelectionWM:GetChildren());

SelectionTitle.Text = toolName;

newItem.Parent = SelectionPreview.Showcase.WorldModel;

newItem.CFrame = CF(CamLV * 4, CamPos);

task.spawn(function()

while task.wait(0.001) and newItem do

newItem.CFrame *= CFrame.Angles(0, 0.05, 0);

end

end);

SPpurchase.MouseButton1Click:Connect(function()

if SPpurchase.Text ~= ("Экипировать" or "Экипировано") and not debounce then

Purchase:InvokeServer("Weapons", toolName);

delay(2, function()

debounce = true;

end);

end;

end);

if Tool.Currency then

SPCurrency.Text = Tool.Currency .. " $";

SPpurchase.Text = "Купить";

else

SPCurrency.Text = "";

SPpurchase.Text = "Экипировать";

end;

if toolDesc then

SelectionDescription.Text = toolDesc;

else

SelectionDescription.Visible = false;

end;

end;

end;

end);

Menu["Open/Close"].MouseButton1Click:Connect(function()

ShopFrame.Visible = not ShopFrame.Visible;

end);

And if someone want to see the DataBase that RemoteFunction taking info from:
Screenshot_1

Thanks for Help!

It for some reason don’t purchases the item, but the script can equip items, if it will find the value with the given property.

can u send the full code? server and client, i think that is better to solve ur problem

ok, there it is:

----Services----
local Players = game:GetService("Players");
local RS = game:GetService("RunService");
local RPS = game:GetService("ReplicatedStorage");
local GS = game:GetService("GuiService");
local SG = game:GetService("StarterGui");
local UIS = game:GetService("UserInputService");
----Modules----
local ToolsModule = require(RPS:WaitForChild("Modules").ToolsModule);
local EmotesModule = require(RPS:WaitForChild("Modules").EmotesModule);
----Variables----
local camera = Instance.new("Camera");
local Template = script.Parent.Shop.PreviewSection.Template;
local ViewPort = Template.Objection;
local CurrentCamera = workspace.CurrentCamera;
local ShopFrame, Preview, Selection, MenuSection = script.Parent.Shop, script.Parent.Shop.PreviewSection, script.Parent.Shop.SelectedSection, script.Parent.Shop.MenuSection;
local Remotes = RPS:WaitForChild("Remotes");
local Player = Players.LocalPlayer;
local EmoteDummy = RPS:WaitForChild("Showcase");
local SelectionPreview = Selection.SelectionPreview;
local Menu = script.Parent.Menu;

local Purchase = Remotes.Purchase;
local EmotePlay = Remotes.PlayEmote;

camera.CameraType = Enum.CameraType.Scriptable;
camera.CFrame = CFrame.new();
Template.Objection.CurrentCamera = camera;
ShopFrame.Draggable = true;

local CamCF = camera.CFrame
local CamLV = CamCF.lookVector;
local CamPos = CamCF.p

local Currency = SelectionPreview.Currency;
local SelectionDescription = SelectionPreview.Description;
local Handler = Preview.Handler;

local CF = CFrame.new;

local SelectionString = "Selection";
local EmoteString = "Emote"

local SelectionTitle = SelectionPreview.Title;
local SelectionShow = SelectionPreview.Showcase;
local SelectionWM = SelectionShow.WorldModel;
local SelectionSelect = Selection.Select;
local SPpurchase = SelectionPreview.Purchase;
local SPCurrency = SelectionPreview.Currency;

local DataBase = Player[Player.UserId];
local Equipped = DataBase["Equipped"];
local Owned = DataBase["Owned"];
----Functions/Connections----
UIS.InputBegan:Connect(function(Input)
	if not Player.Character:GetAttribute(EmoteString) and Input.KeyCode == Enum.KeyCode.G then
		EmotePlay:FireServer();
	end;
end);

local function ClearViewportFrame(viewport)
	for _, obj in pairs(viewport) do
		if obj:IsA("BasePart") or obj:IsA("Model") then
			obj:Destroy();
		end;
	end;
end;

local function ClearFrame(frame)
	for _, obj in pairs(frame) do
		if obj:IsA("Frame") then
			obj:Destroy();
		end;
	end;
	
	SelectionSelect.Visible = true;
	SelectionPreview.Visible = false;
	Selection.BackgroundTransparency = 0.5;
	SelectionDescription.Visible = true;
end;

local function GetType(value: string)
	if EmotesModule[value] then
		return "Emote";
	end;
	
	if ToolsModule[value] then
		return "Tool";
	end;
end;

local function EmoteMenu()
	ClearFrame(Handler:GetChildren());
	for _, emote in pairs(EmotesModule) do
		local emoteAnim = emote.Animation;
		local emoteStart = emote.EmoteStart;
		local emoteCurrency = emote.Currency;
		local emoteName = emote.Name;
		
		local newDummy = EmoteDummy:Clone();
		local newTemplate = Template:Clone();
		
		local newCurrency = newTemplate.Currency;
		local newPreview = newTemplate.Preview;
		local newObject = newTemplate.Objection;
		
		newTemplate.Name = emote.Name;
		newTemplate.Visible = true;
		newTemplate.Parent = Preview.Handler;

		if emoteCurrency then
			newCurrency.Text = emoteCurrency .. " $";
		end;

		newPreview.MouseButton1Click:Connect(function()
			Selection:SetAttribute(SelectionString, emoteName);
		end);

		newDummy.Parent = newObject["WorldModel"];
		newDummy:SetPrimaryPartCFrame(CF(CamLV * 6.5, CamPos));

		if emoteStart then
			emoteStart(newDummy);
		end;
		
		local dummyHum = newDummy.Humanoid;
		local humAnimator = dummyHum.Animator;

		local anim = humAnimator:LoadAnimation(emoteAnim);
		anim.Looped = true;
		anim:Play();
	end;
end;

local function ToolMenu()
	ClearFrame(Handler:GetChildren());
	for _, tool in pairs(ToolsModule) do
		local toolTool = tool.Tool;
		local toolName = tool.Name;
		local currency = tool.Currency;
		
		local newTemplate = Template:Clone();
		local newItem: BasePart = toolTool:Clone();
		local newCurrency = newTemplate.Currency;
		local newPreview = newTemplate.Preview;
		local newObject = newTemplate.Objection;
		
		newTemplate.Name = toolName;
		newTemplate.Parent = Preview.Handler;
		newTemplate.Visible = true;
		
		if currency then
			newCurrency.Text = currency .. " $";
		end;
		
		newPreview.MouseButton1Click:Connect(function()
			Selection:SetAttribute(SelectionString, toolName);
		end);
		
		newItem.CFrame = CF(CamLV * 3, CamPos);
		newItem.Parent = newObject;
		
		task.spawn(function()
			while task.wait(0.001) and newItem do
				newItem.CFrame *= CFrame.Angles(0, 0.05, 0);
			end
		end);
	end;
end;

MenuSection.EmoteSection.MouseButton1Click:Connect(function()
	Preview:SetAttribute("Menu", EmoteString);
end);

MenuSection.ToolSection.MouseButton1Click:Connect(function()
	Preview:SetAttribute("Menu", "Tool");
end);

MenuSection.ToySection.MouseButton1Click:Connect(function()
	Preview:SetAttribute("Menu", "Toy");
end);
----Setting Properties/Attributes----
task.spawn(function()
	repeat task.wait() until ShopFrame.Visible;
	ToolMenu();
end);

Preview.AttributeChanged:Connect(function(attribute)
	if attribute == "Menu" then
		local selection = Preview:GetAttribute(attribute);
		
		if selection == "Emote" then
			EmoteMenu();
		elseif selection == "Tool" then
			ToolMenu();	
		end;
	end;
end);

Selection.AttributeChanged:Connect(function(attribute)
	if attribute == "Selection" then
		--Emote--
		if GetType(Selection:GetAttribute("Selection")) == "Emote" then
			Selection.BackgroundTransparency = 1;
			SelectionSelect.Visible = false;
			SelectionPreview.Visible = true;
			
			local Emote = EmotesModule[Selection:GetAttribute(SelectionString)];
			local emoteName = Emote.Name;
			local emoteStart = Emote.EmoteStart;
			local emoteAnim = Emote.Animation;
			local emoteCurrency = Emote.Currency;
			local emoteDescription = Emote.Description;
			local debounce = false;
			
			local newDummy = EmoteDummy:Clone();
			
			ClearViewportFrame(SelectionWM:GetChildren());
			
			SelectionTitle.Text = emoteName;
			
			newDummy:SetPrimaryPartCFrame(CF(CamLV * 6.5, CamPos));
			newDummy.Parent = SelectionWM;
			
			if emoteStart then
				emoteStart(newDummy);
			end;
			local dummyHum = newDummy.Humanoid;
			local humAnimator = dummyHum.Animator;

			local anim = humAnimator:LoadAnimation(emoteAnim);
			anim.Looped = true;
			anim:Play();
			
			SPpurchase.MouseButton1Click:Connect(function()
				if SPpurchase.Text ~= "Экипировано" and not debounce then
					Purchase:InvokeServer("Emotes", emoteName);
					delay(2, function()
						debounce = true;
					end);
				end;
			end);
			
			for _, value in pairs(Owned["Emotes"]:GetChildren()) do
				if Equipped["Emotes"].Value == emoteName then
					SPpurchase.Text = "Экипировано";
				elseif value.Value == emoteName and Equipped["Emotes"].Value ~= emoteName then
					SPpurchase.Text = "Экипировать";
				else
					SPpurchase.Text = "Купить";
				end;
			end;
			
			if emoteCurrency then
				SPCurrency.Text = Emote.Currency .. " $";
				SPpurchase.Text = "Купить";
			else
				SPCurrency.Text = "";
				SPpurchase.Text = "Экипировать";
			end;
			
			if emoteDescription then
				SelectionDescription.Text = emoteDescription;
			end;
		end;
		--Tool--
		if GetType(Selection:GetAttribute(SelectionString)) == "Tool" then
			Selection.BackgroundTransparency = 1;
			SelectionSelect.Visible = false;
			SelectionPreview.Visible = true;
			
			local Tool = ToolsModule[Selection:GetAttribute("Selection")];
			local toolName = Tool.Name;
			local toolCurrency = Tool.Currency;
			local toolDesc = Tool.Description;
			local debounce = false;
			
			local newItem = Tool.Tool:Clone();
			
			ClearViewportFrame(SelectionWM:GetChildren());
			
			SelectionTitle.Text = toolName;
			
			newItem.Parent = SelectionPreview.Showcase.WorldModel;
			newItem.CFrame = CF(CamLV * 4, CamPos);
			
			task.spawn(function()
				while task.wait(0.001) and newItem do
					newItem.CFrame *= CFrame.Angles(0, 0.05, 0);
				end
			end);
			
			SPpurchase.MouseButton1Click:Connect(function()
				if SPpurchase.Text ~= ("Экипировать" or "Экипировано") and not debounce then
					Purchase:InvokeServer("Weapons", toolName);
					delay(2, function()
						debounce = true;
					end);
				end;
			end);
			
			if Tool.Currency then
				SPCurrency.Text = Tool.Currency .. " $";
				SPpurchase.Text = "Купить";
			else
				SPCurrency.Text = "";
				SPpurchase.Text = "Экипировать";
			end;
			
			if toolDesc then
				SelectionDescription.Text = toolDesc;
			else
				SelectionDescription.Visible = false;
			end;
		end;
	end;
end);

Menu["Open/Close"].MouseButton1Click:Connect(function()
	ShopFrame.Visible = not ShopFrame.Visible;
end);

what is the problem with ur code? dont buying or dont removing the player money?

Don’t buying… But it can equip…

Actually don’t buying and don’t removing player’s money, i’ve checked few minutes ago

so you know how to solve my problem?

nothing in output? ur code works fine, i read it and i dont found any error

1 Like

nothing in output… But i will try to print something when it equips or purchases something

Well, i got it, i don’t need for loops there, they were breaking my script when they find a value with specified value, so i did something like this:

local Players = game:GetService("Players");
local RPS = game:GetService("ReplicatedStorage");
local Purchase = RPS:WaitForChild("Remotes").Purchase;

local function GetType(Type)
	if Type == "Emotes" then
		return require(RPS:WaitForChild("Modules").EmotesModule);
	elseif Type == "Weapons" then
		return require(RPS:WaitForChild("Modules").ToolsModule);
	end;
end;

local function LoopEqual(loop, value)
	for _, obj in pairs(loop) do
		if obj.Value == value then return true; end;
	end;
	return false;
end;

local function CreateNewVal(Name, Value, Parent)
	local newVal = Instance.new("StringValue");
	newVal.Name = Name;
	newVal.Value = Value;
	newVal.Parent = Parent;
end;

Purchase.OnServerInvoke = function(Player, Type: string, Value: string)
	local dataBase = Player[Player.UserId];
	local Owned = dataBase["Owned"];
	local Equipped = dataBase["Equipped"];
	
	local leaderstats = Player["leaderstats"];
	local module = GetType(Type);
	
	if module[Value] then
		local moduleValue = module[Value];
		if Equipped[Type].Value ~= Value and LoopEqual(Owned[Type]:GetChildren(), Value) then
			Equipped[Type].Value = Value;
		end;
		
		if not LoopEqual(Owned[Type]:GetChildren(), Value) and Equipped[Type].Value ~= Value then
			if moduleValue.Currency  then
				local curr = moduleValue.Currency;
				if leaderstats["Снюсы"].Value >= curr then
					leaderstats["Снюсы"].Value -= curr;
					CreateNewVal(Value, Value, Owned[Type]);
					Equipped[Type].Value = Value;
				end;
			end;
		end;
	end;
end;

Thanks for still helping me!

nvm, it broked for some reasons…

i dont see anything broken in ur code

but still something is wrong… There’s no errors

I’ll try something soon, ill do other stuff.

ok, good luck to fix ur code! I hope that u can fix it! :wink:

1 Like