Help on Leaderstats Script

Game for reference: Aim Trainer - Roblox

This is the script:
Workspace>Script

game.Players.PlayerAdded:Connect(function(plr)
local Stats =Instance.new(“Folder”, plr)
Stats.Name = “leaderstats”

local Clicks = Instance.new("IntValue", Stats)
Clicks.Name = "Clicks"
Clicks.Value = 0

end)

This is the Script within the button.

script.Parent.MouseButton1Click:Connect(function()
game.Players.LocalPlayer.leaderstats.Clicks.Value = game.Players.LocalPlayer.leaderstats.Clicks.Value + 1
end)

Is this in a LocalScript? LocalPlayer only works on a LocalScript.

You’d probably need to use RemoteEvents in order to accomplish this instance, since UI Buttons can’t exactly detect server-sided input from the client & changing it from the client would not replicate across the server

Keeping in mind you have to do this through a LocalScript, for the RemoteEvent to detect client-server replication

When a button gets pressed, fire a RemoteEvent from the client to the server to change the stats the way

--Client Side
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
    Event:FireServer()
end)
--Server Side
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(Player) --The player is already passed as the first parameter
    Player.leaderstats.Clicks.Value += 1
end)

Yes it is. Its a local script.

Uhm…I’m not really a programmer. Try out the game because the clicks is not meant to be for a UI.

I’m aware, but why do you say that yet you referenced a MouseButton1Click event, which is meant to be used for GuiObjects?

You also have to reference the Server’s Side (Aka create a Script) in ServerScriptService in order for it to property work

I don’t really know lol. I think I’m confused with the difference of those. Can you help me out here? What script should be used? You don’t need to directly give me the script if you don’t want to. Just guide me on what to script. Thanks! :smiley:

1 Like

Gotcha

So in Lua, we have these things called Events which basically fire depending on what “action” you want it to detect from

Say I have this (Of course just an example):

local AppleTree = workspace.AppleTree

AppleTree.PushedDown:Connect(function()
    print("I've been pushed down >:(")
end)

Since we referenced PushedDown as an event, we have to connect it with a function in order to properly detect whenever it changes

The same goes for MouseButton1Click event, that event fires whenever you click the UI Button with your “Left Mouse” which would fire a RemoteEvent (Which you can create by placing one inside ReplicatedStorage)

After that Event gets fired from the client to the server, we can then create another script to detect when it got fired from as stated from my previous post:

It may look simple, but let’s break it down

OnServerEvent would be considered an Event for detecting who called the FireServer() function, and it has 1 parameter:

  • The Player who fired FireServer() from our Local Side earlier (Or the MouseButton1Click event)

Then we can reference our leaderstats, and the Clicks to increase its Value by 1 hopefully!

1 Like

Ah! Thanks so much! That clears it up! So is the script sent the script I should use? Or is that just an example.

If you’re referring about the 2 scripts that have different replication:

Then yeah, you can put the LocalScript where your UI Button is located, then the ServerScript you can create one & place it inside ServerScriptService so that it detects when FireServer() is called on the client to be able to increase the Clicks value by 1

1 Like

Wait, so MouseButton1Click is for GuiObjects, then what is for the objects in workspace? Like if you click a part then it’ll +1 to leaderstats?

We have our ways :wink:

image

ClickDetectors & ProximityPrompts are nifty Objects we can use that detect when a Player either Mouse Clicks/Keyboard Types, which is the more common use that people use

1 Like

I really am bad a scripting lol. I can’t even process this information.