Hey all, thanks for taking the time to read my post. I have (hopefully) a simple question involving a Client to Server remote event.
I am attempting to make a TextButton that counts up (starting from 0) upon being pressed and then sends the corresponding number of activation’s in a Remote Event to the server.
Here is my LocalScript code. It begins in ReplicatedStorage, but is then cloned into workspace.
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event1 = ReplicatedStorage.PanhandleMoney:FindFirstChild("RemoteEvent1")
local button = script.Parent
local counter = 0
local function activateButton()
print(counter)
counter+=1
event1:FireServer(player, counter)
end
button.Activated:Connect(activateButton)
Here is the corresponding ServerScript:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event1 = ReplicatedStorage.PanhandleMoney:FindFirstChild("RemoteEvent1")
local singleEvent = ReplicatedStorage["Panhandling Remote Events"].singularRemote
local profileCache = require(game.ServerScriptService.ProfileCacher)
local pMoney = require(game.ServerScriptService.ModuleScripts.pMoney)
event1.OnServerEvent:Connect(function(player, counter)
local playerProfile = profileCache[player]
if playerProfile.Data.panhandleUpgrades.guiUpgrades.clickTwice == false and playerProfile.Data.panhandleUpgrades.guiUpgrades.clickThrice == false then
singleEvent:FireClient(player)
local randomInt = tonumber(tostring(Random.new():NextNumber(0, 3)))
playerProfile.Data.Money+=randomInt
local formattedMoney = string.format("%.2f", randomInt)
pMoney:createGUI(player, formattedMoney)
elseif playerProfile.Data.panhandleUpgrades.guiUpgrades.clickTwice == true and playerProfile.Data.panhandleUpgrades.guiUpgrades.clickThrice == false then
print(counter)
local randomInt = tonumber(tostring(Random.new():NextNumber(0, 3)))
playerProfile.Data.Money+=randomInt
local formattedMoney = string.format("%.2f", randomInt)
pMoney:createGUI(player, formattedMoney)
if counter == 2 then
singleEvent:FireClient(player)
end
end
end)
The issue stems from the print() statement located in the ServerScript. It always returns nil, regardless of the arguments entered into the FireServer() command.
I have scanned through devhub posts and tried all following variations of the code:
LocalScript variation:
button.Activated:Connect(function(player, counter)
counter+=1
event1:FireServer(player, counter)
end)
ServerScript variation:
local function giveMoney(player, counter)
local playerProfile = profileCache[player]
if playerProfile.Data.panhandleUpgrades.guiUpgrades.clickTwice == false and playerProfile.Data.panhandleUpgrades.guiUpgrades.clickThrice == false then
singleEvent:FireClient(player)
local randomInt = tonumber(tostring(Random.new():NextNumber(0, 3)))
playerProfile.Data.Money+=randomInt
local formattedMoney = string.format("%.2f", randomInt)
pMoney:createGUI(player, formattedMoney)
elseif playerProfile.Data.panhandleUpgrades.guiUpgrades.clickTwice == true and playerProfile.Data.panhandleUpgrades.guiUpgrades.clickThrice == false then
print(counter)
local randomInt = tonumber(tostring(Random.new():NextNumber(0, 3)))
playerProfile.Data.Money+=randomInt
local formattedMoney = string.format("%.2f", randomInt)
pMoney:createGUI(player, formattedMoney)
if counter == 2 then
singleEvent:FireClient(player)
end
end
end
event1.OnServerEvent:Connect(giveMoney)
I have also tried sending other random parameters, such as strings/Vector3 values. Nil is returned without fail every time. Any advice is appreciated, thank you.