String expected, got nil

Hello, I am making a shop for a game I am working on. Here is the issue:

Thanks for helping!

1 Like

This is due to the InvokeServer method returning nil. Can you provide server sided code?

3 Likes
  local Rep = game.ReplicatedStorage
local ToolData = require(Rep.ToolData)
local Debounce = {}
local MPS = game:GetService("MarketplaceService")

Rep.TriggerTool.OnServerEvent:Connect(function(Player)
	if Player and Player.Character then
		local HasTool = Player.Character:FindFirstChildOfClass("Tool")
		if HasTool and not Debounce[Player.UserId] then
			local Gain = ToolData[HasTool.Name].Gain
			Player.leaderstats.Foods.Value = Player.leaderstats.Foods.Value + Gain
			
			if MPS:UserOwnsGamePassAsync(Player.UserId,5693642) then
				Player.leaderstats.Foods.Value = Player.leaderstats.Foods.Value + Gain
			end
			
			Debounce[Player.UserId] = true
			
			delay(ToolData[HasTool.Name].Debounce,function()
				Debounce[Player.UserId] = false
			end)
			
		end
	end
end)

function Rep.RequestTool.OnServerInvoke(Player,Tool)
	if Player and Rep.Items:FindFirstChild(Tool) and Player.Character and not Player.OwnedTools:FindFirstChild(Tool) and Player.leaderstats.Coins.Value >= ToolData[Tool].Price then
		local Cloned = Rep.Items[Tool]:Clone()
		
		for i,v in pairs(Player.Character:GetChildren()) do
			if v:IsA("Tool") and v.Name ~= "Golden Burger" then
				v:Destroy()
			end
		end
		
		for i,v in pairs(Player.Backpack:GetChildren()) do
			if v:IsA("Tool") and v.Name ~= "Golden Burger" then
				v:Destroy()
			end
		end
		
		local ToolOwned = Instance.new("BoolValue",Player.OwnedTools)
		ToolOwned.Name = Tool
		Cloned.Parent = Player.Backpack
		Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - ToolData[Tool].Price
		Player.CurrentTool.Value = Tool
		return "Bought and Equipped"
	elseif Player and Rep.Items:FindFirstChild(Tool) and Player.OwnedTools:FindFirstChild(Tool) and Player.Character then
		local Cloned = Rep.Items[Tool]:Clone()
		local HasTool = Player.Backpack:FindFirstChildOfClass("Tool") or Player.Character:FindFirstChildOfClass("Tool")
		
		for i,v in pairs(Player.Character:GetChildren()) do
			if v:IsA("Tool") and v.Name ~= "Golden Burger" then
				v:Destroy()
			end
		end
		
		for i,v in pairs(Player.Backpack:GetChildren()) do
			if v:IsA("Tool") and v.Name ~= "Golden Burger" then
				v:Destroy()
			end
		end
		
		Cloned.Parent = Player.Backpack
		Player.CurrentTool.Value = Tool
		return "Equipped"
	elseif Player and Rep.Items:FindFirstChild(Tool) and Player.Character and not Player.OwnedTools:FindFirstChild(Tool) and Player.leaderstats.Coins.Value < ToolData[Tool].Price then
		return "Not enough coin!"
	end
end

That is the whole server-side script

1 Like

So it looks like it’s returning nil because of none of the primary if statements checks are being met. For debugging purposes, print each value the if statement checks and then proceed from there.

1 Like

That shouldn’t happen because I only was working client side and everything from the server side was working properly.

Sometimes code breaks. All I can say is there doesn’t appear to be any other way this could return nil since each of your statements incorporates a return. I suggest proceeding with my debug solution.

1 Like