What does "attempt to index function with 'Connect'" mean in Roblox? - lua

local Player = game.Players.LocalPlayer
local PlayerCash = Player:WaitForChild("leaderstats"):WaitForChild("Strength")
local Mouse = Player:GetMouse()
local amount = 50
script.Parent.Activate:Connect(function()
game.Players.LocalPlayer.leaderstats.money.Value = game.Players.LocalPlayer.leaderstats.money.Value + amount
end)
I am trying to make a code to give Strength when you click. When I press 'Play' it doesn't work. All it says in the output is
'attempt to index function with 'Connect'.'

The error "attempt to index [type] with [field name]" means that you are incorrectly using something like an object, and you are trying to access some field on it.
In your case, this error is pointing at the line : script.Parent.Activate:Connect.This says that script.Parent.Activate is a function and not a table, and you cannot call Connect on a function. This is just to help us figure out what is wrong with our code.
Since you working with Tools, there is a simple fix. Instead of using the Activate function, you are looking for the Activated event. So just change update that line like this :
script.Parent.Activated:Connect(function()

It connects a function to an event that happened, like this:
local tool = script.Parent
-- It connects the event (the tool being equipped) to a function.
tool.Equipped:Connect(function()
-- CODE
end)
Also, the answer above is how you can fix your problem and here's a shortcut: You have defined "Player" and you could've used it inside the function. Have a great day and God bless everyone here.

Related

If a player doesn't own a gamepass, then prompt it via ScreenGUI not working?

I'm trying to make a localscript for a TextButton so that when somebody clicks on it, the script checks if they have the gamepass. If they do, it will do a multitude of other things. But, if the player doesn't have the gamepass, then it will prompt it to them.
Here is the code for my localscript:
local ID = 42076468
local plr = game.Players.LocalPlayer
local MPS = game:GetService("MarketplaceService")
script.Parent.MouseButton1Click:Connect(function()
if MPS:UserOwnsGamePassAsync(plr.UserId,ID) then
-- do stuff
else
MPS:PromptGamePassPurchase(plr.UserId,ID)
end
end)
The problem is that it doesn't prompt the player, even when I remove my conditional statements. This localscript is inside of a TextButton, inside of a Frame, inside of a ScreenGUI.
Any help is deeply appreciated, thanks.
You've made a simple mistake of calling PromptGamePassPurchase(player, gamePassId) the same way as UserOwnsGamePassAsync(userId, gamePassid).
PromptGamePassPurchase expects you to pass in a Player as the first argument, whereas UserOwnsGamePassAsync expects a userId. So all you gotta do is correct the first argument.
MPS:PromptGamePassPurchase(plr, ID)
Also, don't forget to define the callback for the ProcessReceipt event!

LuaU script (Roblox), how can I make a Key get pressed with a script

An example would have been like
local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E)
but it doesnt work ofcourse because i cant jsut make my game press E by itself with this thing, so it requieres soemthing longer and if you find the solution can you also do one where it makes it pressed down?
The input can only be registered on the client, therefore you will have to code in a LocalScript. There are 2 services which are used to get the player's input:-
UserInputService
This example shows how to use the UserInputService in getting the player's LeftMouseButton input.
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed!")
end
end
UserInputService.InputBegan:Connect(onInputBegan)
ContextActionService
This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function ()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
You should take a look at the Wiki pages.
It sounds like you want to write a script to press the E key, but that isn't possible.
You can bind actions to keypresses, just like in the examples Giant427 provided, and you can also manually call the functions that are bound to those actions, but you cannot write a script to fire keyboard inputs.

Why is there an error when I use Roblox Lua Players.LocalPlayer.Mouse:Getmouse()?

I'm trying to make a game but it heavily relies on the mouse but an error occurs saying 'attempt to index nil with 'GetMouse'' and no matter what I do it creates this error. Maybe my game is not updated (I think it is updated). I asked a similar question before but now I'm sure it is Roblox Studio's fault.
Heres the code:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Block = game.ServerStorage.Experimental
function place()
PlacedBlock = Block:Clone()
PlacedBlock.Parent = workspace
PlacedBlock.Position = Mouse.Hit.p
end
Mouse.MouseButton1Click:Connect(place)
The first problem is that MouseButton1Click isn't a valid member of GetMouse(). MouseButton1Click is used for stuff such as GUI objects. Button1Down is used for GetMouse(). Also, .p is deprecated, use .Position instead.
Secondly, you need to place your part in ReplicatedStorage, not ServerStorage. The client can not access ServerStorage. Make sure that you are using a LocalScript.
Fixed:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Block = game.ReplicatedStorage.Experimental
function place()
PlacedBlock = Block:Clone()
PlacedBlock.Parent = workspace
PlacedBlock.Position = mouse.Hit.Position
end
mouse.Button1Down:Connect(place)
Now there's one problem with this. When you place the part, it will only show for you and it wont show for anyone else. To fix this, you will need to use Remote Events.

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.

World of Warcraft Lua - If statement flow

I'm trying to create an addon for World of Warcraft. I have created a function that checks whether a buff has been added to the current player.
Button:RegisterEvent("UNIT_AURA");
local function auraGained(self, event, ...)
if (UnitAura("player", "Heating Up")) then
if (heatingUpIsActive ~= 1) then
heatingUpIsActive = heatingUpIsActive + 1
print (heatingUpIsActive)
end
end
Button:SetScript("OnEvent", auraGained);
This works great, but how do I check if UnitAura is not "Heating Up"?
Also, I would prefer if heatingUpIsActive were a boolean, but it seems to not like when I do that. What is the correct way to create a boolean in Lua?
Your function isn't checking the aura that caused the event. It's looking for "Heating Up" any time any UNIT_AURA event comes by. In fact, it looks like the UNIT_AURA event doesn't actually tell you which aura triggered it. So you can't "check if UnitAura is not "Heating Up"", because you simply don't know what aura caused the event. Perhaps it was even several auras at once.
However, the event does tell you what unit got the aura. It's the first vararg. You should probably check to make sure it's player before doing something
local unitid = ...
if unitid ~= "player" then return end
Also, you didn't explain what problems you had with booleans. You should just be able to say something like
if not heatingUpIsActive then
heatingUpIsActive = true
-- do whatever you want
end
although I never saw any declaration of the variable in your code. It's a bad idea to use globals for things like this, so you should declare
local heatingUpIsActive
before the function declaration, e.g.
local heatingUpIsActive
local function auraGained(self, event, ...)
-- ...

Resources