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.
Related
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)
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.
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
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)
I made a cash for kill script in Roblox, but wanted to go further than that and implement a script where when a player has a gamepass, then that player would recieve more cash than another normal player would.
This is for a battle royale styled game in Roblox, and when I playtested it, there were no errors, but the script didn't work.
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
local increment = 50
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,7382818)then
increment = increment + 50
end
player:WaitForChild("Humanoid").Died:connect(function()
local tag = player.Humanoid:FindFirstChild("creator")
if tag ~= nil then
local killer = tag.Value
if killer ~= nil then
-- Find the killer's leaderstats folder
local killerStats = killer:FindFirstChild("leaderstats")
if killerStats ~= nil then
-- Find the killer's Cash IntValue
local killerCash = killerStats:FindFirstChild("Cash")
-- Increase cash as before
killerCash.Value = killerCash.Value + increment
end
end
end
end)
I expected a VIP player, who has the gamepass, to receive more cash, but when I tested it, no player received any cash for killing another player at all.
How to fix an end expected error near eof
If the Lua interpreter complains about a missing end you`re missing it somewhere.
Read through your code and make sure everything that is supposed to be closed with an end has one. Read through the Lua Reference Manual to find out which keywords need an end.
In your code it's if statements and function definitions. Checking each pair from inside out you'll end up one end) short to close this game.Players.PlayerAdded:connect(function(player) as mentioned in the comments.