Checking if an instance *doesn't* exist -> "Not a valid member" - lua

"checkin is not a valid member of PlayerGui" at line 2
function onClick(plr)
if game.Players[plr.Name].PlayerGui.checkin ~= nil then
print('player already has gui')
else
if game.ServerStorage.Players[plr.Name].Value == 0 then
local gui = game.ServerStorage.GUIs.checkin:Clone()
gui.Parent = plr.PlayerGui
print('fresh gui being handed to '.. plr.Name)
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)

If a member doesn't exist on an instance, Roblox will immediately throw an error.
If you're not sure if a child of a given name exists, use :FindFirstChild(name). Rather than throw an error, it just returns nil.
Note that MouseClick already gives a player, so doing game.Players[plr.Name] is really redundant.
if plr.PlayerGui:FindFirstChild("checkin") then
It's best practice to not handle GUIs on the Server. Instead, you can communicate to LocalScripts what they need to show using RemoteFunctions/RemoteEvents.

Related

“Value is not a valid member of model” error, when it 100% is

So, I’m trying to make it so when a player claims a tycoon, the number of the tycoon is saved in an intvalue that gets parented to their character. There is a button that should only function if the player owns the tycoon it is in. The value is name “Tycoon”. When I try to do
local click = workspace.Button1.ClickDetector
click.MouseClick:Connect(function(player)
if player.Character.Tycoon.Value == 1 then
…
end
end)
the game throws me an error:
“Tycoon is not a valid member of Model “Workspace.PlatinumAdventurer(my user)””
When I run the project, I can see that the intvalue is 100% in the character.
I have tried using :waitforchild, but it doesn’t work. I’ve also tried to, instead of doing player.Character, doing
local playerName = player.Name
…
if workspace.playerName.Tycoon.Value…
Any help would be appreciated, thank you?
It might have to do with the model structure.
You can try checking if player has a character first, then find the IntValueTycoon parented to the character. Once the IntValue is found, it should retrieve it's actual value and perform action if tycoonValue == 1
local click = workspace.Button1.ClickDetector
click.MouseClick:Connect(function(player)
local character = player.Character
if character and character:FindFirstChild("Tycoon") then
local tycoonValue = character.Tycoon.Value
if tycoonValue == 1 then
...
end
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.

attempt to index global 'message' (a nil value) Lua Message script

Currently, I'm working on a simple Lua Roblox script that is supposed to turn the parent part blue when "/blue" is entered in the chat by ANY player. When run, it returns the error "attempt to index global 'message' (a nil value)" in the output. Also, when I hover my cursor over "message" it says "unknown global 'message'". I am sure I'm doing something terribly wrong as I am new to the language. I have tried moving the script into Workspace and Chat (of course changing local part when I do) but those don't help. I'm confident it's a code issue specifically defining a global variable.
local part = script.Parent
local function scan()
if message:sub(1,5) == "/blue" then
part.BrickColor = BrickColor.Blue()
end
end
scan()
First, you didn't define "message" because "message" is supposed to be an argument of
player.Chatted()
So instead of just running scan(), make multiple functions, here is the revised code:
local part = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(message)
message = string.lower(message)
if message == "/blue" then
part.BrickColor = BrickColor.new("Blue")
end
end)
end)
Let me know if you need me to elaborate, I understand that sometimes this stuff can be confusing.

gmod GameMode Lua. IsPlayer retuning nill value

I'm trying to make a gmod gamemode. In my init.lua I wanted it so that way team members can't hurt each other. So I used this code
function GM:EntityTakeDamage( target, dmginfo )
if ( target:IsPlayer() and dmginfo:IsPlayer() ) then
if (dmginfo:Team() == target:Team()) then
dmginfo:ScaleDamage( 0.0 ) // Sets damage to 0
end
end
end
However it's giving me the error telling me that IsPlayer() is a nil value even though it should be returning a boolean. It points to no other lines other then the line with IsPlayer() and it's saying it is IsPlayer()
you have a typo in line 3. dminfo
You should narrow down which of your multiple IsPlayer() calls actually is nil
dmgInfo is a CTakeDamageInfo which has no function IsPlayer()
single line Lua comments are opened with --, not //
https://wiki.garrysmod.com/page/Category:CTakeDamageInfo
If you call a function and it says its nil, then check if it even exists. Or even better, check this befor you use the function in the first place.
And to prevent you from coming back in a minute, CTtakeDamageInfo also does not have a function Team() as well.
Check out CTDamageInfo:GetAttacker()

attempt to call a nil value

for i = 1, groupA:getNumChildren() do
local sprite = groupA:getChildAt(i)
if cute.anim[1]:collidesWith(sprite) then
youLoose()
end
end
local function youLoose()
local font3 = TTFont.new("billo.ttf", 20, " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,?")
local text7 = TextField.new(font2, "gameover")
text7:setPosition(200, 100)
stage:addChild(text7)
GameLost = Bitmap.new(Texture.new("gameover.jpg"))
Background : removeFromParent()
groupA : removeFromParent()
stage: addChild(GameLost)
alert()
end
It gives an error that says 'attempt to call global youLoose (a nil value), where am I doing it wrong?
Note that collideswith is not the same as collidesWith; if that error you posted is correct, then you posted code that is different from what you are using. It could be that the method really is called collidesWith (it appears to be if it is the one from sprite1), but you used collideswith. Alternatively, if the code posted is what you used, then the error is likely attempt to call collideswith(a nil value), so cute.anim[1] is not a sprite1 object, but it is not nil either otherwise the error would be different.
Once you have fixed this, you'll notice that youLoose is defined after that for loop, when you call youLoose() it is not yet defined. You're going to have to move the local function youLoose() function to before the loop. Because the loop is not itself in a function, but is at module level, it gets executed before any following code, so any functions (local or global) that are used in the loop must be defined before the loop.
Note that "loose" does not mean the same as "lose". Check Grammar-monster to see difference. Probably everywhere you have the word "loose" you should change to "lose".

Resources