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

Can't get leaderboard to increase by 1 on click?

I'm not very experienced in scripting and I'm trying to make a script so that the Water Bottle that is in my StarterPack will increase my leaderstats that is "Strength" by 1 every time i left click with it. So basically I need someone to help me make a script so that if I left click the bottle in my inventory, my strength value increases by 1. Thanks!

function DrinkWater(player)
2	    local DrinkWater = player.leaderstats.Strength.Value;
3	    DrinkWater = DrinkWater + 1;
4	    script.Parent.Parent:Destroy();
5	end;
6	 
7	script.Parent.MouseClick:connect(DrinkWater);

I have no clue if this is even CLOSE to right.

2 answers

Note: I wasn't sure whether you were using a ClickDetector or a tool based on your code and your question. This answer includes both scenarios.


ClickDetector

You shouldn't be indexing the Value property of your object. Instead, save the actual object as a variable. Additionally, make sure this is a server script. Local scripts will not run in the workspace.

function DrinkWater(player)
     local DrinkWater = player.leaderstats.Strength
     DrinkWater.Value = DrinkWater.Value + 1
     script.Parent.Parent:Destroy()
end
    
script.Parent.MouseClick:Connect(DrinkWater); --:connect() deprecated

Tool

If you're using a tool, all you have to do is use the Activated event. Additionally, you'll have to use a remote event, as changes to an object inside the player by the client will not replicate to the server.

--Local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent") --wait for remote
local tool = script.Parent --the tool your local script is inside

tool.Activated:Connect(function()
   Remote:FireServer() --you might want to implement a debounce
end)

--Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent") --create remote
Remote.Parent = ReplicatedStorage

Remote.OnServerEvent:Connect(function(player) --listen for event
   if player:FindFirstChild("leaderstats") then
      player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
   end
end)

Also, semicolons are unnecessary in Lua. You can add them like you did freely, but it can make your code appear cluttered sometimes.


##Resources:

Remote Events


##Accept and upvote if this helps!

3 comments

0

Wouldn't the best solution to just have a script in the tool and then have the script change the leaderstats? That way there isn't insanely high remote queues

0

It appears my answer was functional from the beginning, it's a Script-grab. You've been scammed, I assumed that Activated stopped working because that happened a few times during experimental tweaks to the Lua Engine

0

i didnt know there was anything wrong with Activated, but i havent actually been on roblox in a few months, so maybe you're right

Decided I would post my own answer, since I'm not much of a fan of the other two. All you need is a script in your WaterBottle Tool:

local tool = script.Parent
local players = game:GetService("Players")

tool.Activated:Connect(function()
	local pl = players:GetPlayerFromCharacter(tool.Parent)
	if pl then	
		local strength = pl:WaitForChild("leaderstats").Strength
		strength.Value = strength.Value + 1
	end
end)

This will not fill up your remote event queue, which would be a HUGE issue if someone had something like an autoclicker on your game. Although you may want to consider adding your own debounce, as players with the current script could gain thousands of Strength in mere second(s).

2 comments

1

Interesting form of gaining access the Player. Though this is actually not a bad idea.

0

In my opinion, a much better idea than filling up the remote event queue

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