Storing player data from a local script

You are not getting my point. Any solution, you are still passing data from the client to the server. Therefore, any solution will be easily hackable.

If you want to send something to the server from the client, you always have to use a remote. No matter what. If you don’t want to use them, the only solution is to calculate it on the server.

Try handling the saving and loading of player data on the server side.

1 Like

This is possible to be calculated on server, you can fire a remote event to the server when they press start.

how would that work with multiple players at the same time?

You can just start the calculations in the connection where you receive the remote. This will automatically be for each player.

Or… you can start 1 loop and add players to a table.

local P = game:GetService('Players');
local RS = game:GetService('RunService');
local NS = require(game.ReplicatedStorage.numberspinner);

local StartPart = game:GetService('Workspace').MainParts.StartPartTP;

local RealTimeScoreSpinner = NS.fromGuiObject(script.Parent.RealTimeScore);
RealTimeScoreSpinner.Decimals = 0.0;
--Spinner.Prefix = 'Score: ' 
RealTimeScoreSpinner.Prefix = '';
RealTimeScoreSpinner.Duration = 0.25;
RealTimeScoreSpinner.TextScaled = false;
RealTimeScoreSpinner.Parent = script.Parent;

local Elapsed = 0.0;
local Await = 0.1; -- RenderStepped will happen once ever this number of seconds.

RS.RenderStepped(function(DeltaTime: number) -- RenderStepped is preffered over while loops if calculations will last prolonged periods of time. Otherwise you can use while true do loops, but procure the usage of task.wait() instead of wait(), which is deprecated.
        Elapsed += DeltaTime;

        if Elapsed >= Await then
                for _, Player in ipairs (P:GetPlayers()) do
                        local HasHumanoid = pcall(function(): boolean
                                return (Player.Character:FindFirstChild('Humanoid') ~= nil) -- Returns true if the Humanod is different to nil.
                        end);
                        
                        if HasHumanoid then
                                -- Your calculations, unchanged.
                        end
                end
                
                Elapsed = 0.0;
        end
end)

This’d be my way of doing it server-sidedly.

Using a pcall isn’t necessary here, you can just do:

local HasHumanoid = Player.Character and Player.Character:FindFirstChild("Humanoid") ~= nil or false

if HasHumanoid then

end

this is what i came up with, the only problem is that my numberspinner module apparently doesn’t work on the server.

local startpart = game.Workspace:WaitForChild("SpawnLocation")
local startevent = game.ReplicatedStorage.Events.StartGame2
local numberspinner = require(game.ReplicatedStorage.NumberSpinner)

local function round(n)
	return math.floor(n + 0.5)
end

startevent.OnServerEvent:Connect(function(player)
	local ScoreGui = player.PlayerGui.MainMenu.Score
	local RealTimeScore = ScoreGui.RealTimeScore
	local HighScore = ScoreGui.HighScore
	local HighScoreStats = player:WaitForChild("Stats").HighScore	
	local RealTimeScoreSpinner = numberspinner.fromGuiObject(RealTimeScore)
	RealTimeScoreSpinner.Decimals = 0 
	--Spinner.Prefix = "Score: " 
	RealTimeScoreSpinner.Prefix = ""
	RealTimeScoreSpinner.Duration = 0.25
	RealTimeScoreSpinner.TextScaled = false
	RealTimeScoreSpinner.Parent = script.Parent

	HighScore.Text = HighScoreStats.Value
	while wait(.1) do
		if not player.Character:FindFirstChild("Humanoid") then
		else
			RealTimeScoreSpinner.Value = round((startpart.Position - player.Character:GetPivot().Position).Magnitude)
			RealTimeScore.Text = round((startpart.Position - player.Character:GetPivot().Position).Magnitude)
			--warn(HighScore)
			--warn(RealTimeScore.Text)

			if tonumber(HighScore.Text) < tonumber(RealTimeScore.Text) then
				HighScoreStats.Value = RealTimeScore.Text
				HighScore.Text = HighScoreStats.Value
				warn("true "  .. HighScore.Text .. " " .. RealTimeScore.Text)
			else
				warn("false "  .. HighScore.Text .. " " .. RealTimeScore.Text)
			end
		end
	end
end)

--(startpart.Position - player.Character:GetPivot().Position).Magnitude

Using :FindFirstChild() used to throw exceptions for me. I don’t know if this still happens, so I just use pcall(function() end).

No, not anymore, the code I provided should eliminate exceptions.

1 Like

Firstly, you don’t need a function to round numbers up. There’s a Luau built-in one since some time ago: math.round().

Secondly, the error may be either on the module or on your code. It’d be quite weird for it not to work on the server, so I guess it’s on your code. By example, I do not think there’s a child of the Player instance on Players folder called ‘Stats’. Double-check that and use print() statements, breakpoints or whatever to diagnose your error further. We are not at your place. You can’t just wait we magically guess what the error is.

What module are you using? What does your explorer look like? - We do not have such information.

please do not have this mindset and dont try to find workarounds for things even roblox says not to do. handle saving player data from a server script and if you need any local workarounds use a remoteEvent

\

i fixed the numberspinner, since i copied the code from the localscript i forgot to change the parent of the numberspinner

this is what my explorrer looks like:

and the “stats” folder inside the player are the leaderstats.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.