Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my script only printing 0 as the value?

local Players = game:GetService("Players")

local part = script.Parent
local function onTouched(part)
	local playr = Players.LocalPlayer
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if not player then return end
	print(player.leaderstats.Points.Value)
end
part.Touched:Connect(onTouched)

Explanation, the value of Points is set at 0, but when I use a tool to raise the value (similar to a simulator game), it still prints at 0. I even checked in the workspace, even if I SEE it is set at a number higher than 0, it still prints 0.

2 comments

0

If your editing the value on the client, it doesn't replicate to the server.

0

How do I make it replicate to the server?

2 answers

✓ Accepted answer

One likely candidate is that you're attempting to update the leaderstats value through a LocalScript. This does not work because of FilteringEnabled. In short, FilteringEnabled disallows LocalScripts from replicating anything to the server, except for a few rare cases in some Humanoid properties and Tools.

Most games get around this by the use of RemoteEvents. These allow direct communication between the client and the server. Using these, you can have the server update the leaderstats value for the player that fired the remote. I'll drop an example below.

Client Script

workspace.Part.Touched:Connect(function()
	game.ReplicatedStorage.Remote:FireServer()
end)

Server Script

game.ReplicatedStorage.Remote.OnServerEvent:Connect(function(player)
	player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1
end)

Let me explain some things. First off, if you noticed at the top of the server script, you have to define the argument player, as RemoteEvents and RemoteFunctions will always send the player who called :FireServer() or :InvokeServer() as the first argument. And as a word of warning, do not send important data through a RemoteEvent/RemoteFunction. It is very easy for someone with malicious intent to modify the data sent through the remote or send their own :FireServer() calls in order to trick your system.

Hoped this answered any questions you had. If you have any more, you can look up tutorials online or check the developer wiki.

1 comment

0

As he said, be careful with secutity; an exploiter could easily make a "while wait() do Remote:FireServer() end" script.

use remote events. im assuming your editing the value on the client

This discussion is preserved as it was. New answers, comments, and votes are closed.