Leaderstats not working? or just not detecting the click? - lua

I am trying to make a Simulator game on Roblox but I just can't seem to get the leader stats to work or it does work and my click event thing doesn't work, I am just following a tutorial for the scripting so I have no clue. This is my Remotes script where it says print("IS THIS WORKING") that was to see what was the problem. Basically that doesn't run or there's another problem that stops that from running, I think at least. I have other scripts and I will put some in that I think might be neccesary but if you need more feel free to ask me.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local cooldown = 1
print("IS THIS WORKING")
replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce then
debounce.Value = true
player.leaderstats.Stealth.Value = player.leaderstats.Stealth.Value + 25 *(player.leaderstats.Rebirths.Value + 1)
wait(cooldown)
debounce.Value = false
end
Stats
local serverStorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stealth = Instance.new("NumberValue")
stealth.Name = "Stealth"
stealth.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local Folder = Instance.new("Folder")
Folder.Name = player.Name
Folder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = Folder
end)
ModuleScript
local module = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
function module.Lift()
replicatedStorage.Remotes.Lift:FireServer()
end
return module
LocalScript
local module = require(script.Parent:WaitForChild("ModuleScript"))
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Activated:Connect(function()
module.Lift()
end)
For my Explorer Structure click
here

local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
your problem is when it waits for a child it yields the script until RemoteData is found
later in the script, it checks if RemoteData is nil or not
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
two solutions:
first is to add a max wait for the yield so it will eventually stop if it can't find RemoteData (only useful is RemoteData is added after the script starts running)
second is the replace wait with FindFirstChild that will check immediately without waiting for it
the solution I recommend
local remoteData = game:GetService("ServerStorage"):FindFirstChild("RemoteData")
secondary solution if you have RemoteData added after the script starts and want to double-check
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData",20)
be sure to tell me is this works as I spent a while figuring it out

Related

Why LeaderStats folder doesn`t creates| Roblox Studio, Lua

My leaderstats script doesnt work for some reason. I dont know why.
When i testing game in roblox studio folder just doesnt creating.
Because of the bug some information doesn`t loads.
There is the code (Script located in ServerScriptStorage):
wait(1)
print("Script LeaderStats started!")
local dataStoreService = game:GetService("DataStoreService")
local clicksDataStore = dataStoreService:GetDataStore("Clicks")
game.Players.PlayerAdded:Connect(function(Player)
local PStats = Instance.new("Folder", Player)
PStats.Name = "PStats"
local clicks = Instance.new("IntValue", PStats)
clicks.Name = "Clicks"
clicks.Value = 0
local playerUserId = "player_"..Player.UserId
-- loading data
local clicksData
local success, errormessage = pcall(function()
clicksData = clicksDataStore:GetAsync(playerUserId, clicksValue)
end)
if success then
clicks.Value = clicksData
end
end)
-- saving data
game.Players.PlayerRemoving:Connect(function(Player)
local playerUserId = "player_"..Player.UserId
local clicksValue = Player.PStats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end)
game:BindToClose(function(Player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..Player.UserId
local clicksValue = Player.PStats.Clicks.Value
local success, errormessage = pcall(function(Player)
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end
end)
I tried putting it in workspace, but folder didn`t create.
Make sure it's a Server Script inside of ServerScriptService.
replace line 31 with
clicksData = clicksDataStore:GetAsync(playerUserId)
If you want it to show as a leaderboard, replace line 15 with
PStats.Name = "leaderstats"

How do I remove a leaderstats value

I Cant get rid of Mana, I tried to delete the folder and when I went back in the game the folder was still there what should I do?...
CODE--
DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new('IntValue')
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Rebirths = Instance.new('IntValue')
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
-- Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Clicks.Value = data.Clicks
Rebirths.Value = data.Rebirths
-- Set our data equal to the current Clicks
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
Clicks = player.leaderstats.Clicks.Value;
Rebirths = player.leaderstats.Rebirths.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error!")
warn(errormessage)
end
end)
If you can't find the script that creates the leaderstat you could use script like the following to delete the extra leaderstats folder containing the Mana value. I would recommend this because the extra leaderstat has to be generated somewhere and it's best to just get rid of the script creating it.
Anyway you could try this: Add the following code to the ServerScriptService
game.Players.OnPlayerAdded:Connect(function(player)
wait(5) -- to wait for the script that adds the leaderstats to run
for _, child in ipairs(player:GetChildren()) do
if child.Name == "leaderstats" then
for _, value in ipairs(child:GetChildren()) do
if value.Name == "Mana" then
child:Destroy()
end
end
end
end
end)
What the above script does is it checks if a leaderstats folder inside the player exists with the value Mana, if it does, it will destroy the leaderstats folder.
The code posted above is not adding the extra leaderstats folder and value mana. User the search in the explorer to find any scripts in your game. Just type script into your search. Then open each file in your game hit Ctl + F on your keyboard and find Mana in each script.
Why this could happen? There are many assets from the toolbox that come bundled with scripts which could be one cause of your problem, but it can also happen because of plugins you have installed which add leaderstats to your game.

My cash value is suppose to go up every minute but its not. (Roblox Studio lua error)

I was trying to re-edit the code over and over again but it still didn't work I've created the folder leader stats and when I play the game it shows that it's a part of the player. It says however that it isn't a valid member.
The other error says: Cash is not a valid member of Folder "Players.(players name).leaderstats"
It's because game.Players.PlayerAdded is an event which you're assigning to a variable.
Try this for the PlayerAdded script (you will need that cash add function in this script):
local players = []
game.Players.PlayerAdded:Connect(function(ply)
table.insert(players, ply)
end)
while true do
wait(60)
for i, v in ipairs(players) do
v:WaitForChild("leaderstats").Counter.Value += 1
end
end
I've written this from my memory as I am away from a PC that can test this code so best of luck!
while wait(60) do
for i, v in ipairs(game.Players:GetChildren()) do
if v:FindFirstChild("leaderstats") then
if v.leaderstats:FindFirstChild("Counter") then
v.leaderstats.Counter.Value += 1
end
end
end
end
You always need to make sure what you're using exists. If you want to avoid errors, I prefer using FindFirstChild instead of WaitForChild to not get it into an infinite wait incase it somehow doesn't load.
it looks like you forgot to create the leaerstats folder. Here is a fixed code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
while true do
task.wait(60)
Counter.Value += 1
end
end)
This will start counting time after player joins. If you want to increase counter value of all players at the same time, use this code:
local PlayersService = game:GetService("Players")
local Players = {}
PlayersService.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
table.insert(Players, player)
end)
while true do
task.wait(60)
for _, player in ipairs(Players) do
player.leaderstats.Counter.Value += 1
end
end
You don't need to check if "Counter" or "leaderstats" exist as they are created before the player is being inserted into the table.

How do i fix this spinning?

The code:
-- yes i know i am using an inefficient loop but if i dont use one everything breaks
local TweenService = game:GetService("TweenService")
while game:GetService("RunService").RenderStepped:Wait()do
local part = script.Parent
local goal = {}
goal.Position = game.Players.LocalPlayer.Character:FindFirstChild("Portal_Gun"):WaitForChild(script.Parent.Name).Position
local goal2 = {}
goal2.Orientation = game.Players.LocalPlayer.Character:FindFirstChild("Portal_Gun"):WaitForChild(script.Parent.Name).Orientation
local tweenInfo = TweenInfo.new(0)
local tween = TweenService:Create(part, tweenInfo, goal)
local tweenInfo2 = TweenInfo.new(.1)
local tween2 = TweenService:Create(part, tweenInfo2, goal2)
tween:Play()
tween2:Play()
end
Note that there is 6 clones of this script and that i am not meaning to break any copyrights
Note 2. The scripts are a descendant of a ViewportFrame
I made an script that uses TweenService which ended up causing a problem
I expected it to work good enough
The problem

im making a roblox game and need to update the leaderboard stats when you click the baseplate

this is my code at the moment:
local players = game:WaitForChild("Players")
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local baseclicks = Instance.new("IntValue", stats)
baseclicks.Name = "baseclicks"
stats.Parent = player
baseclicks.Value = 100
end
players.PlayerAdded:connect(createLeaderboard)
Im not sure if i need a clickdetector with a script inside or??
i dont know, please help.
If you don't want ClickDetectors on the Baseplate, simply, you could use mouse.Target. For example:
-- serverscript
local players = game:GetService("Players");
local function createLeaderboard(player);
local stats = Instance.new("Folder", player);
stats.Name = "leaderstats";
local baseclicks = Instance.new('IntValue', stats);
baseclicks.Name = 'baseclicks'
baseclicks.Value = 100;
end
-- localscript in startercharacterscript
local players = game:GetService("Players");
local client = players.LocalPlayer;
local mouse = client:GetMouse(); --method for getting the client's mouse
local event = game.ReplicatedStorage.OnClick --our remote event
local leaderstats = client.leaderstats or client:WaitForChild("leaderstats");
local clickvalue = leaderstats.baseclicks or leaderstats:WaitForChild("baseclicks");
mouse.Button1Down:Connect(function()
if not (mouse.Target) then return; end
if (mouse.Target.Name == "Baseplate") then
event:FireServer(clickvalue.Value + 1); --fire the remote event
end
end
end)
-- server script in serverscriptservice for remote event receiver
game.ReplicatedStorage.OnClick.OnServerEvent:Connect(function(Player, Value)
Player.leaderstats.baseclicks.Value = Value;
end
However, when it comes to the game having multiple players clicking at once, remote events are not recommended as they are more than likely going to lag the server or cause network traffic - you're better off using ClickDetectors.

Resources