Why isn't this Roblox function/script working? - lua

I started with Roblox a few months ago and my only semi-related prior experience is in web development, so you're dealing with a noob here.
I'm having trouble awarding my game's currency to the players of the winning team of a round. Every other aspect of my currency seems to work fine - it displays, can update, saves with datastores etc.
This in-game currency is set up in the same manner as leaderstats (but not as a leaderstats) - it's created under the player at runtime (if that's the proper way of saying it) via a script in SSS.
The function I'm having issues with doesn't seem to do anything, and I've tried about 20 different variations, some of which were radically different from this. The one below is the last one I tried before giving up to seek help here.
function goldToWinningTeam()
local winningTeam = TeamManager:GetWinningTeam()
for i, Player in pairs(Players:GetPlayers()) do
if player.TeamColor == winningTeam.TeamColor then
player.currency.Gold.Value += 100
else
player.currency.Gold.Value += 50
end
end
end
This doesn't produce any output errors or interfere with other game logic as far as I can tell.
At the moment, for testing, the function above is in my main game script (a regular script). Its proper/ideal location, upon getting it to work, is also something I'd appreciate some feedback on. I was thinking either in my TeamManager or PlayerManager module scripts, or possibly my GameCurrecy script, which is a regular script in SSS.
Below are the TeamManager function(s) called from above. I've tried using either. Both work as expected for their intended purpose of displaying a victor at round end. I figured they would also work in this use case.
function TeamManager:GetWinningTeam()
local highestScore = 0
local winningTeam = nil
for _, team in ipairs(Teams:GetTeams()) do
if TeamScores[team] > highestScore then
highestScore = TeamScores[team]
winningTeam = team
end
end
return winningTeam
end
-- Below works identically for displaying victor
function TeamManager:HasTeamWon()
for _, team in ipairs(Teams:GetTeams()) do
if TeamScores[team] >= Configurations.DESTROYS_TO_WIN then
return team
end
end
return false
end
Again, I'm very new to this and it's only a hobby, please forgive any obvious ignorances.

Related

Can't figure out what to hook into in order to add text into GameTooltip when hovering over applicants in LFG

I'm making an addon that adds some information to the LFG both when applying to them and when picking applicants for your own group. There are different functions and different templates for the frames for searching LFG and hosting LFG. For searching, you can use hooksecurefunc on LFGListUtil_SetSearchEntryTooltip and that will allow you to get the information on whatever group the player is hovering over.
hooksecurefunc("LFGListUtil_SetSearchEntryTooltip", function(gametooltip, resultID, autoAcceptOption)
--debugPrint("LFGListUtil_SetSearchEntryTooltip")
local entry = C_LFGList.GetSearchResultInfo(resultID)
if not entry or not entry.leaderName then
return
end
local skill, attitude, note, playerNumber = self:getPlayer(self:addServerName(entry.leaderName))
--debugPrint("hooked correctly!")
GameTooltip:AddLine("\n")
if skill ~= 0 then
GameTooltip:AddDoubleLine("Skill:", tostring(skill), 1,0.82,0.0,1,1,1)
end
if attitude ~= 0 then
GameTooltip:AddDoubleLine("Attitude:", tostring(attitude), 1,0.82,0.0,1,1,1)
end
if note ~= "" then
GameTooltip:AddLine(note,1,1,1,true)
end
end)
This method seems to almost work when you're looking for applicants to your own group. According to Wow's UI source xml, the function "LFGListApplicantMember_OnEnter" triggers whenever you enter a frame made with the LFGListApplicantMemberTemplate. As far as I can tell from the LFGList's .lua file, all of the members of an applying group should have this template. However, it only triggers when hovering over the not-leading member of a group or when hovering over their role icons.
Is there some other function I'd be able to hook into in order to make it work on the leading group? I know it's possible to add text to the tooltip of a leading member because the RaiderIO addon does it, but I can't figure out how they do it. It needs to have some way of getting the name of the player you're hovering over to use my getPlayer function on it.
This is my current test function, though obviously it doesn't work
hooksecurefunc("LFGListApplicantMember_OnEnter", function(...)
GameTooltip:AddLine("")
GameTooltip:AddLine("TestText")
debugPrint("Hooked Successfully")
end)
and this is debugPrint
local function debugPrint(...)
print(...)
end

attempt to index nil with 'leaderstats' in roblox studio

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)

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

GMOD Lua Use type doesn't matter?

recently I have been creating an imitation HarborRP gamemode for Garry's Mod, and I'm trying to recreate the Smuggler NPC (You'll know what I mean if you've ever played HarborRP) So basically I want the NPC to open ONE Derma-Frame window when the player presses their use key on it. I have the NPC created and all of that, but when the player simply presses their use key on the NPC, a million windows pop up, I have the NPC/Entity's use type set to SIMPLE_USE, but it seems like that doesn't matter because so many windows pop up. the VGUI/Derma Frame's settings is set to MakePopup() but that doesn't matter either. See if you can find whats wrong with it, I have very little knowledge of LUA.
init.lua file:
include("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
util.AddNetworkString("smug")
hook.Add("PlayerUse", "menuu", function(ply, ent)
net.Start("smug")
net.Send(ply)
end)
function ENT:Initialize()
self:SetModel('models/humans/group01/female_01.mdl')
self:SetHullType(HULL_HUMAN)
self:SetHullSizeNormal()
self:SetNPCState(NPC_STATE_SCRIPT)
self:SetSolid(SOLID_BBOX)
self:SetUseType(SIMPLE_USE)
self:DropToFloor()
end
cl_init.lua file:
include("shared.lua")
function ENT:Draw()
self.Entity:DrawModel()
end
net.Receive("smug", function()
if( !frame ) then
local frame = vgui.Create("DFrame")
frame:SetSize(900,600)
frame:SetPos(ScrW()/2-450,ScrH()/2-300)
frame:SetVisible(true)
frame:MakePopup()
frame:SetDeleteOnClose(true)
elseif (frame) then print("HI")
end
end)
shared.lua file:
ENT.Base = "base_ai"
ENT.Type = "ai"
ENT.PrintName = "[DEV] Smuggler NPC"
ENT.Category = "InVaLiD's HBRP Entities"
ENT.Author = "InVaLiD"
ENT.Spawnable = true
ENT.AdminSpawnable = true
ENT.AutomaticFrameAdvance = true
Something To Note
All of these files are in the addons/smug_npc/lua/entities/ folder.
Yes, I know I have weird names for things, that's just me.
I have a basic to no knowledge of lua, so explain things please :)
I really do appreciate your help, and your will to help people, please know that I thank you all for being here just to sort other people's problems even though you could be spending your time doing something productive, Thank you!
You need to put your code that does net.Send into the entities ENT:Use function rather than on the PlayerUse hook.
function ENT:Use(ply)
net.Start("smug")
net.Send(ply)
end
The below line which you already have in your code in the initialize function makes the entity call the ENT:Use function once when a player presses E on it, which it seems that you want to do so that is good:
self:SetUseType(SIMPLE_USE)
Also, I recommend checking out the gmod forums if you need developer help in future.
GMod forums: https://gmod.facepunch.com/f

In Lua, using a boolean variable from another script in the same project ends up with nill value error

This code is for a modding engine, Unitale base on Unity Written in Lua
So I am trying to use a Boolean Variable in my script poseur.lua, so when certain conditions are met so I can pass it to the other script encounter.lua, where a engine Predefined functions is being uses to make actions happens base on the occurring moment.
I tried to read the engine documentation multiple times, follow the exact syntax of Lua's fonction like GetVar(), SetVar(), SetGobal(),GetGlobal().
Searching and google thing about the Language, post on the subreddit and Game Exchange and tried to solve it by myself for hours... I just can't do it and I can't understand why ?
I will show parts of my codes for each.
poseur:
-- A basic monster script skeleton you can copy and modify for your own creations.
comments = {"Smells like the work\rof an enemy stand.",
"Nidhogg_Warrior is posing like his\rlife depends on it.",
"Nidhogg_Warrior's limbs shouldn't\rbe moving in this way."}
commands = {"GREET", "JUMP", "FLIRT", "CRINGE"}
EndDialougue = {" ! ! !","ouiii"}
sprite = "poseur" --Always PNG. Extension is added automatically.
name = "Nidhogg_Warrior"
hp = 99
atk = 1
def = 1
check = "The Nidhogg_Warrior is\rsearching for the Nidhogg"
dialogbubble = "rightlarge" -- See documentation for what bubbles you have available.
canspare = false
cancheck = true
GreetCounter = 5
Berserk = false
encounter:
-- A basic encounter script skeleton you can copy and modify for your own creations.
encountertext = "Nidhogg_Warrior is\rrunning frantically"
nextwaves = {"bullettest_chaserorb"}
wavetimer = 5.0
arenasize = {155, 130}
music = "musAncientGuardian"
enemies = {"poseur"}
require("Monsters.poseur")
enemypositions = {{0, 0}}
-- A custom list with attacks to choose from.
-- Actual selection happens in EnemyDialogueEnding().
-- Put here in case you want to use it.
possible_attacks = {"bullettest_bouncy", "bullettest_chaserorb", "bullettest_touhou"}
function EncounterStarting()
-- If you want to change the game state immediately, this is the place.
Player.lv = 20
Player.hp = 99
Player.name = "Teemies"
poseur.GetVar("Berserk")
end
Thank you for reading.
The answer to my problem was to use SetGobal(), GetGobal().
For some reasons my previous attempt to simply use SetGobal()Resulted in nil value despite writing it like that SetGobal("Berserk",true) gave me a nill value error, as soon as I launch the game.
But I still used them wrong. First I needed to put it SetGobal() at the end of the condition instead of at the start of the the poseur.lua script because the change of value... for some reasons was being overwritten by it first installment.
And to test the variable in the function in my encounter.lua, I needed to write it like that
function EnemyDialogueStarting()
-- Good location for setting monster dialogue depending on how the battle is going.
if GetGlobal("Jimmies") == true then
TEEEST()
end
end
Also any tips an suggestions are still welcome !
Well firstly, in lua simple values like bool and number are copied on assignment:
global={}
a=2
global.a=a--this is a copy
a=4--this change won't affect value in table
print(global.a)--2
print(a)--4
Secondly,
SetGobal and the other mentioned functions are not part of lua language, they must be related to your engine. Probably, they use word 'Global' not as lua 'global' but in a sense defined by engine.
Depending on the engine specifics these functions might as well do a deep copy of any variable they're given (or might as well not work with complicated objects).

Resources