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)
It keeps returning nil for player and saying that im trying to index a nil with 'WaitForChild' even though I have tried adding a wait command and changing player to 'game.Players.LocalPlayer' I'm new to scripting and I don't know what else to do.
local buyButton = script.Parent
local player = game:GetService("Players").LocalPlayer
local multiplier = player:WaitForChild("Multiplier")
buyButton.MouseButton1Up:Connect(function()
if multiplier.Value == 0 then
multiplier.Value = 1
end
end)
Instead of local player = game:GetService("Players").LocalPlayer you want to do:
local players = game:GetService("Players")
local player = players.localplayer
I can not resolve this by my self.
Thank You for helping.
This is my first stackoverflow question :3
Roblox Studio information:
Latest available version as of 2022 October 03
Picture of about window:
https://i.stack.imgur.com/Wo6Br.png
https://i.stack.imgur.com/v7Jot.png
The output:
ServerScriptService.CheckpointsScript:31: attempt to index nil with 'leaderstats'
The code:
local Players = game:GetService("Players")
local CheckpointsFolder = game.Workspace.Checkpoints
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = leaderstats
--player.CharacterAdded:Connect(function(char)
-- local checkpoint = CheckpointsFolder:FindFirstChild(Stage.Value)
-- player.Character..CFrame = CFrame.new(checkpoint.PrimaryPart.CFrame.Position.Y + 5 =, checkpoint.PrimaryPart.CFrame.Rotation)
--end)
end)
for _, v in pairs(CheckpointsFolder:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
local stageValue = player.leaderstats.Stage
if player and stageValue.Value < tonumber(v.Name) then
stageValue.Value = v.Name
end
end)
end
end
Your issue is coming from this line
local stageValue = player.leaderstats.Stage
And that is because the Touched event fires for any object that touches it. And you need to make sure that player actually exists.
You are doing this already, you just need to move this line inside your safety check.
local player = Players:GetPlayerFromCharacter(part.Parent)
if player then
local stage = player.leaderstats.Stage
local currentStage = tonumber(v.Name)
if stage.Value < currentStage then
stage.Value = currentStage
end
end
I am trying to clone an object from the replicatedstorage to the players backpack when an object part is touched and the code looks fine for me but it keeps giving a nil response from clone.parent = player.backpack
local replicatedtorage = game:GetService("ReplicatedStorage")
local Sword = replicatedtorage:FindFirstChild("Sword")
local part = game.Workspace.Part
local player = game.Players.LocalPlayer
local clone = Sword:Clone()
part.Touched:Connect(function(hit)
local humanoid = hit.parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
clone.Parent = player.Backpack
end
end)
This looks like a server Script which cannot access Players.LocalPlayer like clients can because there is no local player to the server. A way to get the Player that touched a part is through Players:GetPlayerFromCharacter() which requires one instance to be passed and will either return the Player whose Character is that instance or nil.
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
clone.Parent = player.Backpack
end
end)
This should work right away in your script and can replace your existing Touched connection.
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)