Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my script only printing 0 as the value?

Asked by 4 years ago
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.

0
If your editing the value on the client, it doesn't replicate to the server. mybituploads 304 — 4y
0
How do I make it replicate to the server? Dockboy20006 10 — 4y

2 answers

Log in to vote
0
Answered by
ArtBlart 533 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
As he said, be careful with secutity; an exploiter could easily make a "while wait() do Remote:FireServer() end" script. mybituploads 304 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Answer this question