roblox studio script problems - lua

I am making a game where when a part at the front of a car touches another part, the other part becomes unanchored. I am making a different script that gives the player a coin every time they do this. Yes, I have already made a leaderstats script. code:
function onPartTouched(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end

You do actually have to attach the function lol
function onPartTouched(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
MyPart.Touched:Connect(onPartTouched)

Related

Roblox Soccer Game: How can I connect my leaderboard stats to a soccer ball touching the goal?

how can I connect the ball touching the goal box, to my player score? I tried this, but the ball touching the goal box does not seem to be registering.
This script is under the score manager I created, in ServerScriptService.
'''
local Players = game:GetService("Players")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local goal = Instance.new("IntValue")
goal.Name = "Goals"
goal.Value = 0
goal.Parent = leaderstats
end
Players.PlayerAdded:Connect(leaderboardSetup)
local part = workspace.Goal1
local otherPart = workspace.Pelota
local function onPartTouch(part, otherPart)
print (part.Name .. "collided with " .. otherPart.Name)
local ball = otherPart.Parent:FindFirstChild("Part")
local player = Players:FindFirstChild("Humanoid")
if ball then
player.leaderstats.Goals.Value = player.leaderstats.Goals.Value + 1
end
end
part.Touched:Connect(onPartTouch)
'''

I'm creating this Roblox game but I don't want the user to have multiple of the same items

I'm creating a shop for my game and I don't want the user to be able to buy the same item multiple times my script works fine when the item isn't equipped but when it is I'm able to buy another one.
This is my current script
local function buy (plr)
local leaderstats = plr:FindFirstChild('leaderstats')
if leaderstats then
local coins = leaderstats:FindFirstChild('Murabux')
if not plr:WaitForChild("Backpack"):FindFirstChild("Katana") then
if coins and coins.Value >= 90 then
coins.Value = coins.Value - 90
local clone = game:GetService('ReplicatedStorage').Katana:Clone()
clone.Parent = plr.Backpack
end
end
end
end
Your code is lacking a check for the held item from the player, which is under their character.
Correction:
local function buy (plr)
local leaderstats = plr:FindFirstChild('leaderstats')
if leaderstats then
local coins = leaderstats:FindFirstChild('Murabux')
if not plr:WaitForChild("Backpack"):FindFirstChild("Katana") and not plr.Character:FindFirstChild("Katana") then
if coins and coins.Value >= 90 then
coins.Value = coins.Value - 90
local clone = game:GetService('ReplicatedStorage').Katana:Clone()
clone.Parent = plr.Backpack
end
end
end
end

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.

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.

How do I make a Leaderboard on roblox?

How do I make a Leaderboard on roblox?
In every player needs to be inserted a value called 'leaderstats', using a script with the PlayerAdded event. Inside the leaderstats value, you can place IntValues - their name is what will show as the heading, and their value is what will appear as the player's stat.
To make those stats change, you need to add different functions and/or events to the script that created the leaderstats values.
Insert a script into workspace, then in the code type this:
function Onplayerentered(player)
local leaderstats = Instance.new("IntValue")
leaderstats.Parent = player
leaderstats.Value = 0
leaderstats.Name = "leaderstats"
local stat = Instance.new("IntValue")
stat.Name = "" -- Put name here in between the quotations
stat.Value = -- Put the starting Value#
end
game:GetService("Players").ChildAdded:Connect(Onplayerentered)
Bring up to roblox insert toolbar.
Select Leaderbord.
You can customize the script to fit your needs!
Roblox leaderboards is a very long script, thankfully, the script allow us to easily add and remove leaderstats. To add a leaderboard insert a IntValue inside of the player object, to add a stat insert a IntValue inside the leadestats.
Most games on Roblox want every player to have the same leaderboard. So most people use a PlayerAdded event and create the leaderboard
Insert a script into ServerScriptService and paste down the following code:
plrEntered = function(plr)
local ls = Instance.new('IntValue') --Leaderstats
ls.Parent = plr
ls.Value = 0
ls.Name = 'leaderstats'
local stat = Instance.new('IntValue')
stat.Name = 'Money' -- Change to the value you want
stat.Value = 0 -- Add the starting value
end
game:GetService'Players'.PlayerAdded(plrEntered)
ROBLOX defines a leaderboard as an object that is named as 'leaderstats' and is located in the player object. A leaderboard statistic is defined as a value object inside the leaderstats object (Player>leaderstats>ValueObject). So lets write a function that creates a leaderboard with a 'cash' statistic for a player.
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local cash = Instance.new("IntValue", stats)
cash.Name = "Cash"
stats.Parent = player
end
Then we need to make this work. We need to connect this function to the 'PlayerAdded' event from the 'Players' object.
local players = game:WaitForChild("Players")
players.PlayerAdded:connect(createLeaderboard)
And that is basically it.
Note that line 3 in the code shown directly above is the equivalent of:
players.PlayerAdded:connect(function(player)
createLeaderboard(player)
end)
The entire script would look like this:
local players = game:WaitForChild("Players")
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local cash = Instance.new("IntValue", stats)
cash.Name = "Cash"
stats.Parent = player
end
players.PlayerAdded:connect(createLeaderboard)
It is recommended to put the script in the 'ServerScriptService'.
function Onplayererntered(player)
local leaderstats = Instance.new("IntValue")
leaderstats.Pareny = player
leaderstats.Value = 0
leaderstats.Name = "leaderboard"
local stat = Instance.new("IntValue")
statname = "Cash"
stat.Value = 100
end
you first have to make a script inside the server script service and name it what ever you want, and write this in the script (make sure its normal script not local)
game:GetService("Players").PlayerAdded:Connect(function() --make the function start when new player joins
local player = game.Players.PlayerAdded --make player variable
local leaderstats = instance.new("Folder", player) --make new folder and set it's parent to the player
local money = instance.new("IntValue", leaderstats) --create new value for the stat and set it's parent to the leaderstats folder (you can create as many as u want)
money.name = "Money" --make the name of the value
money.Value = 0 --make the value's value
end)
this block of code is simple and has many comments to explain it I wish it was helpful.
function stats(plr)
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local coins = Instance.new("IntValue")
Coins.Name = "coins"
Coins.Parent = leaderstats
end)
game.Players.PlayyerAdded:Connect(stats)

Resources