The following script breaks between print("2") and print("3") because it cannot find the variable "tag". How would I fix this?
local Humanoid = script.Parent.Zombie -- Or Zombie Or Whatever
function PwntX_X()
print("1")
local tag = Humanoid:findFirstChild("creator")
print("2")
if tag ~= nil then
print("3")
if tag.Value ~= nil then
print("4")
local Leaderstats = tag.Value:findFirstChild("leaderstats")
print("5")
if Leaderstats ~= nil then
print("6")
Leaderstats.Cash.Value = Leaderstats.Cash.Value + 5
print("7")
wait(0.1)`enter code here`
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
I already have a script for the leaderboard that works 100%. This script is being used for a game called "ROBLOX". Thanks!
Alright, first off, scriptinghelpers.org was created as a service similar to stackoverflow specifically for Roblox, I suggest next time, you ask there, now on to stuff;
-- If you have further problems, My Roblox username is 'ZeroBits' (REMOVE THIS LINE)
DEBUG = true -- Debug Print, so you can disable it when you're done.
local function print_D(t)
if DEBUG == true then warn(t) end end
print_D("DEBUG MODE, Switch Debug Value to False when finished")
local Humanoid = script.Parent:FindFirstChild("Zombie") -- We'll use FindFirstChild() here
--if not Humanoid then -- Uncomment this statement if you want it to affect objects named Humanoid as well
-- Humanoid = script.Parent:FindFirstChild("Humanoid")
--end
function HumanoidKilled() -- Renamed the function to be a little less cringeworthy
print_D("1") -- switched these print statements with the Print_D() function
local tag = Humanoid:FindFirstChild("creator") -- Capitalized the first letter in FindFirstChild()
print_D("2")
if not tag then
warn("No Tag Found check Humanoid and weapon script") --if this prints, there's either no tag, or a serious problem, and you should check the humanoid object for a tag, or make sure the weapons you use actually generate tags.
else -- changed 'if tag ~= nil then' to 'if not tag then 'do stuff' else' to simplify the code, and add an else statement early on.
print_D("3")
if tag.Value then -- removed '~= nil' because it's just useless clutter
print_D("4")
local Leaderstats = tag.Value:findFirstChild("leaderstats")
print_D("5")
if Leaderstats ~= nil then
print_D("6")
Leaderstats.Cash.Value = Leaderstats.Cash.Value + 5
print_D("7")
wait(0.1)
script:Destroy() -- switched remove() to Destroy(), remove() is deprecated
end
end
end
end
Humanoid.Died:connect(HumanoidKilled)
This fixes all the problems in the script, and makes it more efficient. if there are still problems, it's either in the tag creation script, the tags aren't stored in the humanoid, or the tags aren't named 'creator'
also, I switched over the print statements to a print_D function, which uses warn() rather than print() there's almost no difference, except that the debug text will appear yellow in the console, rather than white.
script is being used for a game called "ROBLOX". Thanks!
It's for a game On Roblox, not a game called Roblox, what you said is akin to saying; I made this mod for Source Engine, when you actually made the mod for Half-Life 2
Related
local find = script.Parent
find.Touched:Connect(function(touched)
local de = find:FindFirstChild("Humanoid")
if de == true then
print("we found a human!")
end
end)
is not working?? I'm new to this but i just don't understand!
The reason why your script is not functioning as intended is because :FindFirstChild() will return an object. (not a boolean)
So your statement is practically stating
local part = Instance.new("Part")
if part == true then -- Part does not equal true
The solution is quite simple. When :FindFirstChild() doesn't find anything it will return nil. So just make sure that it `~=~ nil
local find = script.Parent
find.Touched:Connect(function(touched)
local de = touched.Parent:FindFirstChild("Humanoid")
if de ~= nil then -- checking if a humanoid was found
print("we found a human!")
end
end)
If you are trying to detect if a player was joined then Try use this code:
local find = game:GetService("Players")
find.PlayerAdded:Connect(function ()
print("we found a human")
end)
Copy it into your LocalScript and place your LocalScript in StarterGui library
Learn more about Players here -
Learn more about LocalPlayer/Player here
If that's not what you meant to, I will edit the answer.
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.
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.
i'm making a script that detects a person using their name however I can't seem to get it working. Please also point out any mistakes I have done in my script it would help a lot.
game.Workspace:WaitForChild("Console")
print("Waited")
game.Players.PlayerAdded:Connect(function(plr)
print("Connected")
if game.Workspace.Console and plr.Name == "wojciechpa2007" then
local Console = game.Lighting.Console:Clone()
Console.Parent = plr.Startergui
print("Cloned")
elseif
not game.Workspace.Console and plr.Name == "wojciechpa2007" then
plr.Startergui.Console:Destroy()
print("Destroyed")
end
end)
Heyo,
This script has a race-condition in it. Your first line game.Workspace:WaitForChild("Console") will block execution of the rest of your script until the object is loaded, or a timeout is reached.
This means that it is possible that a player could join the game before the script can listen for the game.Players.PlayerAdded signal.
Also StarterGui does not exist on specific player. It exists at the game level and is a bucket that dumps its stuff into a Player's PlayerGui when that player's character loads into the game.
So to fix your script, you could try something like this :
-- right away connect to the PlayerAdded signal
game.Players.PlayerAdded:Connect(function(plr)
print("Player Joined!", plr.Name, plr.UserId)
-- do something special if wojciechpa2007 joins
if plr.Name == "wojciechpa2007" then
print("wojciechpa2007 joined! Adding console!")
-- add the special console into the player's PlayerGui for when they load
local Console = game.Lighting.Console:Clone()
Console.Parent = plr.PlayerGui
end
end)
Some recommendations and things to be careful about here :
It's safer to check a player's UserId than it is to check their name. Roblox lets you change your name, but you UserId is always the same.
Putting something into your StarterGui will make it show up in your PlayerGui the next time your character loads. But if your character is already loaded, you won't see it until the next time you respawn.
If your Console object is some kind of GUI element, make sure that it is parented to a ScreenGui object before you insert it into the Player. Otherwise it just won't show up.
Hope this helps!
-- This is some of the code made by Roblox themselves:
-- Setup table that we will return to scripts that require the ModuleScript.
local PlayerStatManager = {}
-- Table to hold all of the player information for the current session.
local sessionData = {}
-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
sessionData[player][statName] = sessionData[player][statName] + changeValue
end
-- Function to add player to the sessionData table.
local function setupPlayerData(player)
sessionData[player] = {Money = 0, Experience = 0}
end
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)
-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager
--------------------------------------------------------------------------------
-- Require ModuleScript so we can change player stats
local PlayerStatManager = require(game.ServerStorage.PlayerStatManager)
-- After player joins we'll periodically give the player money and experience
game.Players.PlayerAdded:connect(function(player)
while wait(2) do
PlayerStatManager:ChangeStat(player, 'Money', 5)
PlayerStatManager:ChangeStat(player, 'Experience', 1)
end
end)
When I run these two script, it run perfectly, adding the print(sessionData[player][statName]) inside the ChangeStat function, but when I removed the game.Players.PlayerAdded:connect(setupPlayerData) part in the module script, it stopped working. I though module script does not execute code without it being called, and if that was the case, shouldn't the game.Players.PlayerAdded:connect(setupPlayerData) part be delay and not function since player's already added, therefore it not firing?
require executes the required code.
If that was not the case you would not be able to get a table through require, as your return PlayerStatManager statement would not be executed.
As a consequence removing
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)
will cause an added player not to be initialized properly. This basically says: when a new player is added, call setupPlayerData.
Where setupPlayerData says: give a new set of stats to player
As you removed that line no player has stats. If you don't have stats you can't increase their values...
So obviously you did not understand what the code did befor you changed it. Therefor you cannot understand why your changes cause problems.
If you change a system you don't understand you can be lucky, but in most cases you will utterly fail.