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

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)

Related

attempt to index nil with 'Character' Error Roblox Studio

Hello i am trying to get the position of the HumanoidRootPart but it says attempt to index nil with 'Character' also the FollowPlrbox has the players name.(This not a local script)
Script:
script.Parent.MouseButton1Click:Connect(function()
local fwbox = script.Parent.Parent.FollowPlrbox.Text
local player = game.Workspace:FindFirstChild(fwbox)
local plrpart = player.Character:FindFirstChild("HumanoidRootPart")
local plrposx = plrpart.Position.X
print(plrposx)
end)
Error:
attempt to index nil with 'Character'
Your error is telling you that player doesn't exist. This could mean that whatever you have typed for the player's name is misspelled, incorrect capitalization, or there's simply no player with that name.
Anyways, you need to account for the fact that the player might not exist before trying to access properties on it. Also, since you are searching the Workspace, you are not looking for a Player, you are looking for that player's character model.
So to fix your issue, you simply need to add safety checks.
script.Parent.MouseButton1Click:Connect(function()
local fwbox = script.Parent.Parent.FollowPlrbox.Text
local character = game.Workspace:FindFirstChild(fwbox)
if character then
local rootPart = character:FindFirstChild("HumanoidRootPart")
if rootPart then
local rootPartPosX = rootPart.Position
print(rootPartPosX)
end
end
end)
The reason we add if character then and if rootPart then is because it's possible that the FindFirstChild() function won't find anything. So we say, only do the rest of this code if character exists.

Why is my script not being able to kick a user?

This script is designed so that when you say "!kick [user]" it kicks that user from the game. But, when I test it out, nothing happens with no errors in the output box. Whats going on?
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(player,message)
if player.Name == "playername" then
local words = string.split(message," ")
if string.lower(words[1]) == "!kick" then
words[2]:Kick("You have been kicked.")
end
end
end)
end)
Thanks
The reason nothing is happening right now is because your very first check is failing. You have the parameters for the Player.Chatted connection backwards. The message comes first and the recipient comes second. So what you've named player is actually the message and if player.Name == "playername" then is likely failing because strings don't have a Name property and player.Name is nil.
After that, the next issue you will run into is that after you've split the command into two parts, the second half of the command is still a string, and not a Player. So you need to find a Player with a matching name as the input string.
Try something like this:
local Players = game.Players
local function findPlayerByName(name)
-- escape if nothing is provided
if name == nil then
return nil
end
-- check if any players match the name provided
local allPlayers = Players:GetPlayers()
for _, player in ipairs(allPlayers) do
if player.Name == name or string.lower(player.Name) == name then
return player
end
end
-- couldn't find any players that matched
return nil
end
-- define all the players that can use commands
local admins = {
["playername"] = true,
}
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message, recipient)
if admins[player.Name] then
local words = string.split(message, " ")
local cmd = string.lower(words[1])
if cmd == "!kick" then
local playerName = words[2]
local targetPlayer = findPlayerByName(playerName)
if targetPlayer then
targetPlayer:Kick("You have been kicked.")
end
end
end
end)
end)

Why do keep getting this error, I'm trying to turn a character invisible when the function runs

local camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local startergui = game:GetService("StarterGui")
local char = Players.LocalPlayer.Character
local model = workspace.OceanVillagedr201["Wine Cellar"].WineDigitalkeypad
local screen = model.screen
local replicated_storage = game:GetService("ReplicatedStorage")
local CheckCode = Instance.new("RemoteEvent")
CheckCode.Name = "CheckWineCellarCodeEvent"
CheckCode.Parent = replicated_storage
local function Entercode(player)
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
game.StarterGui = replicated_storage.EnterWineCellarCode
end
screen.ProximityPrompt.Triggered:Connect(function(player)
Entercode()
end)
Im attempting to create a function that triggers when the proximity prompt is triggered, One piece of this Entercode() function is to toggle the playermodel's Transparency from 0 to 1 and Remove the Characters ability to move.
local function Entercode(player)
print("went")
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
But Im having trouble with this Piece. It keeps telling me "attempt to index nil" with anything dealing with trying to reference the Characters model. (FFC(), GetChildren(), Player.LocalPlayer.Character, etc.). I am using a local script because I plan to create a Remote Function for the result of EnterCode()
I believe your problem is that the character either was not spawned at the time of making char or is an old character (player respawned & a new character was made). A quick fix would be to redeclare char inside Entercode:
local function Entercode()
char = player.Character
char.Humanoid.RootPart.Anchored = true
for _, p in pairs(char:GetChildren()) do
if p:IsA("BasePart") then
p.Transparency = 1
end
end
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
end
There were also a number of other errors, here are a few changes I made:
local function Entercode()
In the original code, player was a parameter but was not sent as an argument Good thing you set the player variable at the beginning of the code.
if p:IsA("BasePart") then
p.Transparency = 1
end
In the original code, you didn't check to see if p was a part or not.
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
In the original code, you tried to set StarterGui to EnterWineCellarCode? I don't know what you were going for but I'm assuming you meant to parent EnterWineCellarCode to PlayerGui
Lastly, you might want to use GetDescendants() instead of GetChildren(). To better understand how the character works I recommend you read the wiki entry for it

How to give money to player which username is stored in a string value - Roblox lua

So I'm making a game on Roblox where you can pick up objects and sell them. however, the selling script is a normal Script (ran on the server), and I can't use
Game:GetService("Players").LocalPlayer
to find who to give the money to. the object that the user picks up has an
Owner
value, and when they touch it, it changes it to their username. so when the object touches the selling part, it looks at the owner, and gives the money to them (which is stored in a number value called Owner).
Here is my current code:
local part = script.Parent
local function onPartTouched(otherPart)--otherPart is the part that touched it
if otherPart:FindFirstChild("Owner") == nil then
else
local owner = game.Players..otherPart.Owner.Value
getowner.leaderstats.Bucks.Value = getprice.leaderstats.Bucks.Value + otherPart.Price.Value
otherPart.Parent = game.ServerStorage
end
end
part.Touched:Connect(onPartTouched)
you can just do game.Players[value.Value]
just use
Game.Players.LocalPlayer
also just writing #agents answer too in proper format.
local part = script.Parent
local function onPartTouched(otherPart)
if otherPart:FindFirstChild("Owner") == nil then
else
local owner = game.Players[otherPart.Owner.Value]
getowner.leaderstats.Bucks.Value = getprice.leaderstats.Bucks.Value +otherPart.Price.Value
otherPart.Parent = game.ServerStorage
end
end
part.Touched:Connect(onPartTouched)

Roblox How to detect a player's team

I am making a Roblox game, and I need to detect a player's team somehow.
My code currently looks like this:
script.Parent.Touched:Connect(function(part)
local plr = part.Parent.Name
if (game.Players:FindFirstChild(plr).Team == "Police") then
....
end
end)
And when I touch that part (it's an invisible wall), it gives me an error: Workspace.Part.Script:3: attempt to index a nil value
What am I doing wrong?
Edit: I found out it can't find my name in game.Players, because now I tried:
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:FindFirstChild(hit.Parent.Name)
if (plr.Team == "Police") then
...
And now I get Workspace.Part.Script:3: attempt to index local 'plr' (a nil value)
Edit2: Now I tried printing plr (game.Player:FindFirstChild(hit.Parent.Name)) and it was ' Miniller', not 'Miniller', and now I didn't get any errors, but the code below also did nothing..
I solved it by not using variables AND not "Police", it's game.Teams.Police, so code:
if (game.Players:FindFirstChild(hit.Parent.Name).Team = game.Teams.Police
...

Resources