My lua code does not invoke the remotefunction.It only outputs Clicked and BuyNormalCase when pressed.It does not print out the result nor were there errors.Could anybody help me out?Any help would be appreciated
–local script
TrailCase.MouseButton1Click:Connect(function()
local Type = "Trails"
print("Clicked")
local Result = BuyNormalCaseRemote:InvokeServer(Type)
print("Clicked2")
print(Result)
end)
–script
BuyNormalCase.OnServerInvoke = function(player,Type)
print("Invoked")
return CheckModule.BuyNormalCase(player,Type)
end
Module script–
BuyNormalCase = function(plr,Type)
print("yes")
local Token = plr.Token
local CasePrice = 0
local TypeTable = {
["Trails"] = "OwnedTrails";
["Tags"] = "OwnedTitles";
["Capes"] = "OwnedCapes";
}
local Type = game.ReplicatedStorage:FindFirstChild(Type)
if Token.Value >= CasePrice then
Token.Value = Token.Value -CasePrice
local Capes = game.ReplicatedStorage.Capes
local Tags = game.ReplicatedStorage.Titles
local Trails = game.ReplicatedStorage.Trails
local Table = {}
for i,v in pairs(Type) do
table.insert(Table,v)
end
local rarityTable = {
["Common"] = 60,
["Uncommon"] = 15,
["Rare"] = 10,
["Epic"] = 4,
["Legendary"] = 1
}
local weight = 0
local RandomNumber = math.random(1,100)
local counter = weight + RandomNumber
print(counter)
for rarity, weight in pairs(rarityTable) do
if RandomNumber <= counter then
local newtable = {}
for i,v in pairs(Table) do
local rarity = v.Rarity.Value
if rarity == not rarityTable[counter] then
print("not the correct rarity")
else do
table.insert(newtable,v)
local Reward = newtable[math.random(1,#newtable)]
print(Reward)
local new = Reward:Clone()
new.Parent = plr[TypeTable[Type]]
return Reward.Name
end
end
end
end
end
end
end,
A client local script, referencing the RemoteFunction and Invoking Server, printing the answer form server:
local serverResponse = game.ReplicatedStorage:WaitForChild("RemoteFunction"):InvokeServer("calling")
warn(serverResponse)
A server script that reads the invoke and return an answer:
game.ReplicatedStorage:WaitForChild("RemoteFunction").OnServerInvoke = function(player, message)
warn(message)
return "server answer"
end
@FangScripting what you mean by having a FireClient() ? Thats a RemoteFunction, no need to do that from server script, remote function will reply to the client after Invoke
If the result is not needed, we recommend that you use a RemoteEvent instead, since its call is asynchronous and doesn’t need to wait for a response to continue execution. See Remote Functions and Events.
In this script i am using a remotefunction.The problem is whenever i invoke the server through a local script nothing is printed from the server side when the script is invoked.
A local script in StarterCharacter scripts, referencing a part in workspace to click, InvokingServer (in this basic example there is a RemoteFunction in ReplicatedStorage called “RemoteFunction” and a part in workspace named “TrailCase”):
local BuyNormalCaseRemote = game.ReplicatedStorage:WaitForChild("RemoteFunction")
game.Workspace.TrailCase.ClickDetector.MouseClick:Connect(function()
local Type = "Trails"
local Result = BuyNormalCaseRemote:InvokeServer(Type)
print("Module sent this thru server script:", Result)
end)
A server script handling the Invoke and calling a Module named “CheckModule”:
local BuyNormalCaseRemote = game.ReplicatedStorage:WaitForChild("RemoteFunction")
local CheckModule = require(game.ServerScriptService:WaitForChild("CheckModule"))
BuyNormalCaseRemote.OnServerInvoke = function(player, Type)
print("Invoked by", player)
-- CALLING MODULE TO SEND ANSWER TO CLIENT
return CheckModule.BuyNormalCase(player, Type)
end
A module named CheckModule with a function in it called “BuyNormalCase” it should perform what you want and send the result back (its placed in ServerScriptService the same as the server script):
local CheckModule = {}
CheckModule.BuyNormalCase = function(plr, Type)
print("module doing stuff", plr, Type)
-- DO STUFF
-- SEND BACK ANSWER FROM MODULE TO SERVER SCRIPT
return "Stuff Done"
end
return CheckModule
@FangScripting I guess indeed OP wants to get a response from server with data from the module