here it is, it is roblox code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local player = game:GetService("Players")
local leaderstats = player:WaitForChild('leaderstats', 10)
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local function clicksStore()
clicks.Value = clicks.Value + 1
if rebirths <= 1 then
clicks.Value = clicks.Value + rebirths.Value
end
end
remoteEvent.OnServerEvent:Connect(clicksStore)
is there anyway i can fix this? its getting on my nerves. Thank you for helping!
You are indexing the leaderstats correctly, but in a server Script, you aren't accessing the player correctly.
You can easily access the player by using the RemoteEvent's OnServerEvent connection.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local function clicksStore(player)
local leaderstats = player.leaderstats
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local clickValue = 1 + rebirths.Value
clicks.Value += clickValue
end
remoteEvent.OnServerEvent:Connect(clicksStore)
Related
I am trying to make a leader board that tells the player how much cats they have collected. the player has a file and the script counts how many items the file has then displays it on the leaderboard, but it doesn't work(shows the leaderboard but no second row for the amount collected)! Any ideas??
here is my code:
`local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder", player)
folder.Name = "leaderstats"
local catsamount = Instance.new("IntValue", folder)
catsamount.Name = "Cats Found"
catsamount.Value = 0
while wait(0.1) do
catsamount.Value = 0
for _, v in pairs(catscollected:GetChildren()) do
catsamount.Value = catsamount.Value + 1
end
end
end)`
You have to parent catsamount to folder and folder to player:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local catsamount = Instance.new("IntValue", folder)
catsamount.Name = "Cats Found"
catsamount.Value = 0
catsamount.Parent = folder
while wait(0.1) do
catsamount.Value = 0
for _, v in pairs(catscollected:GetChildren()) do
catsamount.Value = catsamount.Value + 1
end
end
end)
This is so that the folder will be added to the player after it is created - Instance.new() creates an instance, but it does not parent the created instance to anything so it will not appear anywhere.
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')
-- Erstellt Leaderboard
local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local clicks = Instance.new('IntValue')
clicks.Name = 'Clicks'
clicks.Value = 0
clicks.Parent = leaderstats
local rebirths = Instance.new('IntValue')
rebirths.Name = 'Rebirths'
rebirths.Value = 0
rebirths.Parent = leaderstats
-- Wenn ein Nutzer verlassen hat das die daten gespeichert wurden
local playerUserId = 'Player_'..player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
clicks.Value = data['Clicks']
rebirths.Value = data['Rebirths']
else -- Wenn user das erste mal joint das value 0 ist
clicks.Value = 0
rebirths.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function ()
local playerUserId = 'Player_'..player.UserId
playerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
I tried to make a DataStore that stores the values of the leaderboard into a data that returns everytime if i rejoin. But it doesnt work and idk why. Im kinda new into lua programming but u guys can help me :D thanks.
The script is very messy due to multiple attempts of trying to fix it from various places and people, yet it led to square one each time...
Local script:
local Cash500 = script.Parent
local A500 = script.Parent.A500
local player = game.Players.LocalPlayer ReplicatedStorage = game:GetService("ReplicatedStorage")
MPS = game:GetService("MarketplaceService")
id = 3736929511
local player = game.Players.LocalPlayer
local remoteEvent = ReplicatedStorage:WaitForChild("RE500")
--Enlarges size when mouse hovers
script.Parent.MouseEnter:Connect(function(x, y)
Cash500.Size = UDim2.new (0, 90, 0, 85)
A500.Size = UDim2.new(0, 75, 0, 30)
end)
--Resets size on not hovering mouse after mouse hovered
script.Parent.MouseLeave:Connect(function(x, y)
Cash500.Size = UDim2.new (0, 75, 0, 70)
A500.Size = UDim2.new(0, 60, 0, 15)
end)
--Purchase 500 money for 35 robux
--Fires remoteEvent when clicked- goes to RemoteEventController that gives money over
Cash500.MouseButton1Click:Connect(function()
MPS:PromptProductPurchase(player, id)
remoteEvent:FireServer("RE500")
end)
Leaderstat Handler:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", leaderstats)
Money.Name = "Money"
local Gems = Instance.new("IntValue", leaderstats)
Gems.Name = "Gems"
end)
local function addTime(player)
while true do
local RandomNumber = math.random(1,5)
wait(3)
player.leaderstats.Money.Value+=RandomNumber -- Same as Value=value+1 it is now- Value+=1
end
end
game.Players.PlayerAdded:Connect(addTime)
RemoteEvent Controller:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MPS = game:GetService("MarketplaceService")
local remoteEvent = ReplicatedStorage:WaitForChild("RE500")
local function Additional500(player)
print(player.Name .. " fired the remote event")
print("Updated! <3")
end
MPS.ProcessReceipt = function(receiptInfo)
print("OOOF")
if receiptInfo.ProductId == 3736929511 then
print("Start!")
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 500
print("Ended!")
return Enum.ProductPurchaseDecision.PurchaseGranted
end
end
remoteEvent.OnServerEvent:Connect(Additional500)
There are no error messages coming out of the output but this when you click it comes up:
The Error message
Anyone know what to do ? Thanks! :)
I tried to make the functions via Datastore so that it only saved the data Wins and reset the stage always but saved Wins always so if I have 250 stages and 1 win and I leave the game I will have 0 stages and 1 win.
local CheckpointService = {}
-- SERVICES
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
-- MODULES
local AchievementService = require(game.ServerScriptService.Server:WaitForChild("Achievement"))
-- VARIABLES
local CHECKPOINTS = game.Workspace:WaitForChild("Checkpoints")
local STAT_NAME = "Stage"
local WINS_NAME = "Wins"
local PREVENT_SKIPPING = false
-- CONNECTIONS
local function playerAdded(player)
local leaderstats = Instance.new("Folder", player) or player:FindFirstChild("leaderstats")
leaderstats.Name = "leaderstats"
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = STAT_NAME
checkpointStat.Value = 1
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = WINS_NAME
checkpointStat.Value = 0
player.CharacterAdded:connect(function(character)
repeat
wait()
until CHECKPOINTS and checkpointStat
local goto = CHECKPOINTS:FindFirstChild("Checkpoint " .. checkpointStat.Value)
if goto then
character:MoveTo(goto.Position)
else
warn("[SERVER] - Checkpoint " .. checkpointStat.Value .. " not found!")
end
end)
end
When I kill an NPC, my score should increase, but it does not.
Here's my code. It is located under my NPC model.
local Humanoid = script.Parent.Humanoid
function PwntX_X()
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats ~= nil then
Leaderstats.Score.Value = Leaderstats.Score.Value + 250 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
And here's my code for the leaderboard
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue",plr)
stats.Name = "leaderstats"
local score = Instance.new("IntValue", stats)
score.Name = "Score"
score.Value = 0
end)
Not sure, but looks like
score.Value = Leaderstats.Score.Value