attempt to index nil with 'leaderstats' in roblox studio - lua

local garbage = game.Teams["Glizzy Garbage"]
local player = game.Players.LocalPlayer
if player.leaderstats.Pounds.Value <= 1000 then --this is the line that the output is detecting the error
player.Team = garbage
end
I am trying to make it where when the player reaches a certain amount of 'pounds' then they will automatically receive a roll. I've searched through many youtube videos and haven't found a fix or an alternative way to do this, and I'm not sure why this isn't working. This script is located in the workspace. All help is appreciated.

The LocalPlayer object is only exposed in LocalScripts. Because you're using a Script in the Workspace, you'll have to access the player object another way. It's also a good idea to handle this kind of logic inside a function that is fired any time the Pounds value changes.
Try using the game.Players.PlayerAdded signal :
local garbage = game.Teams["Glizzy Garbage"]
game.Players.PlayerAdded:Connect(function(player)
local Pounds = player.leaderstats.Pounds
Pounds.Changed:Connect(function(value)
if value <= 1000 then
player.Team = garbage
end
end)
end)

Mine is just
local plrStage = plr.leaderstats.Stage.Value

try instead of putting team name put team colour
also use
player.Team.Service = garbage
end)

Related

Script doesn't seem to be working, and I cannot understand why

script dont work
I am incredibly new to scripting, and I was just fooling around in ROBLOX studio and I made a script that was INTENDED to put an item stored in ReplicatedStorage into a specific players StarterPack, but no matter what I change it does not want to work.
Could anyone explain why?
local playerName = "plr name"
local toolName = "tool name"
game.Players.PlayerAdded:Connect(function(player)
if player.Name == playerName then
local tool = game.ReplicatedStorage:FindFirstChild(toolName)
player.StarterPack:AddItem(tool)
end
end)
You are using an :AddItem() function which doesn't work. You must first clone the tool and add it into the players inventory.
Check the documentation on StarterPack if you are having issues: https://create.roblox.com/docs/reference/engine/classes/StarterPack
Try this:
local playerName = "plr name"
local toolName = "tool name"
local tool = game.ReplicatedStorage:FindFirstChild(toolName)
game.Players.PlayerAdded:Connect(function(player)
if player.Name == playerName then
local newTool = tool:Clone()
newTool.Parent = player.Backpack
end)
end`
The StarterPack is used to determine a set of Tools that all players will spawn with. If a developer wants to ensure that certain Tools are available to specific players, then they will need to parent the Tools directly to the player's Backpack instead.
Your code is mostly fine, but you didn't close your PlayerAdded function, and the StarterPack doesn't have an AddItem function.
If you are having issues, you can always check the documentation for the StarterPack. One issue that I think you'll run into is that putting a tool into the StarterPack will give it to all players, not just the one who matches the name.
The StarterPack is used to determine a set of Tools that all players will spawn with. If a developer wants to ensure that certain Tools are available to specific players, then they will need to parent the Tools directly to the player's Backpack instead.
So instead let's clone the tool into the player's Backpack. But since the backpack gets cleared every time the player respawns, let's move this code into the CharacterAdded event, so it fires every time the character loads into the Workspace.
local playerName = "plr name"
local toolName = "tool name"
local tool = game.ReplicatedStorage:FindFirstChild(toolName)
game.Players.PlayerAdded:Connect(function(player)
if player.Name == playerName then
player.CharacterAdded:Connect(function()
local newTool = tool:Clone()
newTool.Parent = player.Backpack
end)
end
end)

Cloning Locally and not locally [Lua]

I've been trying to make a sword fight game on Roblox studio. I've made a shop Gui so you can click the text button to buy the sword. It works well, You click it checks your kills and if you have enough you get the weapon. In this case, it's 0 kills. But when you take out the sword you cant use it. I've done my research and I believe that's because it's been cloned locally and not globally. Is this the case and if so how do I fix it?
The script in the Text Button:
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Kills.Value >= 0 then
local clonar = game.ServerStorage.ClassicSword:Clone()
clonar.Parent = player.Backpack
end
end)
Thanks in advance!
When work needs to be performed on the server (as opposed to locally on the client), you can use RemoteEvents to communicate across the client-server boundary.
So first, you'll need to create a RemoteEvent in a shared location, like ReplicatedStorage.
Next, update your client-side LocalScript to fire the RemoteEvent :
local player = game.Players.LocalPlayer
local buyEvent = game.ReplicatedStorage.RemoteEvent
script.Parent.MouseButton1Click:Connect( function()
buyEvent:FireServer()
end)
Finally, you need to create a Script in the Workspace or ServerScriptService that listens for that RemoteEvent and performs the work of giving the player the item :
local buyEvent = game.ReplicatedStorage.RemoteEvent
buyEvent.OnServerEvent:Connect( function(player)
if player.leaderstats.Kills.Value >= 0 then
local clonar = game.ServerStorage.ClassicSword:Clone()
clonar.Parent = player.Backpack
end
end)

Roblox Lua - attempt to index nil with 'stats' (leaderstats)

i want to make this, when the baseFinal is touched by a block (Bloque) it gives you money and the block is destroyed. it gives me an error: attempt to index nil with 'stats'
local base = script.Parent.Base
local baseFinal = script.Parent.Final
local plr = game.Players.LocalPlayer
baseFinal.Touched:Connect(function(hit)
if hit.Name == "Bloque" then
wait(0.6)
plr.stats.Value = plr.stats.Value + 5 // here is the error
hit:Destroy()
end
end)
The error is telling you that the plr variable is undefined or nil.
Since this code is running in a Script, the issue is how you are accessing the Player object. See the documentation for Players.LocalPlayer :
This property is only defined for LocalScripts (and ModuleScripts required by them), as they run on the client. For the server (on which Script objects run their code), this property is nil.
The way to fix this is to access the Player object another way. One way is to connect to the Players.PlayerAdded signal.
local base = script.Parent.Base
local baseFinal = script.Parent.Final
local connections = {}
game.Players.PlayerAdded:Connect( function(plr)
-- listen for Players to touch the block
local connection = baseFinal.Touched:Connect( function(hit)
if hit.Name == "Bloque" then
wait(0.6)
plr.stats.Money.Value = plr.stats.Money.Value + 5
hit:Destroy()
end
end)
-- hold onto the connection to clean it up later
connections[plr] = connection
end)
-- clean up when the Player leaves
game.Players.PlayerRemoving:Connect( function(plr)
connections[plr]:Disconnect()
end)
This is most likely because when you are trying to reference a value, you must put .Value after it in order to change the value itself. Assuming you have a stats folder, you should use plr.stats.Value.Value instead.
Next time, please show us your object structure so we have a better understanding of what the error is. Thanks.

Why is there an error when I use Roblox Lua Players.LocalPlayer.Mouse:Getmouse()?

I'm trying to make a game but it heavily relies on the mouse but an error occurs saying 'attempt to index nil with 'GetMouse'' and no matter what I do it creates this error. Maybe my game is not updated (I think it is updated). I asked a similar question before but now I'm sure it is Roblox Studio's fault.
Heres the code:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Block = game.ServerStorage.Experimental
function place()
PlacedBlock = Block:Clone()
PlacedBlock.Parent = workspace
PlacedBlock.Position = Mouse.Hit.p
end
Mouse.MouseButton1Click:Connect(place)
The first problem is that MouseButton1Click isn't a valid member of GetMouse(). MouseButton1Click is used for stuff such as GUI objects. Button1Down is used for GetMouse(). Also, .p is deprecated, use .Position instead.
Secondly, you need to place your part in ReplicatedStorage, not ServerStorage. The client can not access ServerStorage. Make sure that you are using a LocalScript.
Fixed:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Block = game.ReplicatedStorage.Experimental
function place()
PlacedBlock = Block:Clone()
PlacedBlock.Parent = workspace
PlacedBlock.Position = mouse.Hit.Position
end
mouse.Button1Down:Connect(place)
Now there's one problem with this. When you place the part, it will only show for you and it wont show for anyone else. To fix this, you will need to use Remote Events.

How do I make this code function correctly?

I want to make a sort of Lobby system in Roblox Studio where if you have 4 people on a part you get sent to another place. I tried to set up a system for it, but it didn't work; can you help me through this?
I've tried making it so it says .Value at the end.
local TeleportService = game:GetService("TeleportService")
player_amount = script.Parent.Parent.Parent.Player_Count
local placeID_1 = 4119652438
local function onPartTouch(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
player_amount.Value = player_amount.Value + 1
end
if player_amount == 4 then
TeleportService:Teleport(placeID_1, player)
end
end
script.Parent.Touched:Connect(onPartTouch)
I expected the output to be 0 then if one person steps on it, it would update the sign to say 1. But it only stays at 0.
This is not a viable solution as .Touched fires every frame the player touches a part, and only when they move. I suggest creating a hitbox, as I have done here

Resources