problem with event parameters / arguments. (Roblox Studio) - lua

Basically I have a code that gets fired to a localscript in all players. The speaker / player argument is used to make sure the event is recieved by the correct reciever, and the "allow" argument is a boolean value used for later code. (Don't think about that part).
This is the :FireClient Script
1 game.ReplicatedStorage.AllowDisallow:FireClient(speaker, true)
This is the reciever / localscript
1 game.ReplicatedStorage.AllowDisallow.OnClientEvent:Connect(function(player, allow)
2 if not player.Name == game.Players.LocalPlayer.Name then return end
Error:
attempt to index boolean with 'Name'
My thought is that it erases the player parameter and switches it with the 'allow' bool value. I have tried searching this issue up on google, but I did not find a clear answer.
OTHER INFORMATION:
There's an event in ReplicatedStorage, which is the one im using.
It's called "AllowDisallow". Don't think too much about the name.
The 'allow' argument should be a bool value, and the "player" should be the player.
Sincerely, Mathe

The error is telling you that the player variable in the LocalScript is a boolean, not a Player object.
When you call a RemoteEvent's FireClient function, the first argument indicates which player it is going to, and all the rest are provided as the arguments in the OnClientEvent callback. For example,
-- if the server calls...
RemoteEvent:FireClient(player, a, b, c)
-- the client will receive...
RemoteEvent.OnClientEvent:Connect(function(a, b, c) end)
You just need to update your receiver LocalScript so that the arguments reflect what the server is providing. Then you can remove the LocalPlayer safety check, because the OnClientEvent only fires for the LocalPlayer.
local allowDisallow = game.ReplicatedStorage.AllowDisallow
allowDisallow.OnClientEvent:Connect(function(allow)
-- do some stuff
end)

try to print out the arguments in the server event and check whats wrong

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!

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

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.

Bailing people out of prison in roblox using Remote Events doesn't seem to work properly

Here's the deal. When you get arrested in my game, you get sent to jail. To get out you must be bailed out.
The client sends a request to the server to bail them. Every other part seems to work except this part I believe, however it could be the client side script. Is there anything incorrect about this script? I have checked it for any errors that are obvious to me.
local replicatedStorage = game:GetService('ReplicatedStorage')
local createSystemMessage = replicatedStorage:WaitForChild('CreateSystemMessage')
game.ReplicatedStorage.Bail.OnServerEvent:Connect(function(Player,PlayerToBail)
Player = game.Players:FindFirstChild(Player)
local tab = nil
for i,v in pairs(_G.GlobalData) do
if v.Name == Player.Name then
tab = v
end
end
if PlayerToBail.Team == game.Teams:FindFirstChild("Criminal") then
local Bounty = PlayerToBail.leaderstats.Bounty.Value * 2
if tab.Bank <= Bounty then
tab.Bank -= Bounty
PlayerToBail.leaderstats.Bounty.Value = 0
PlayerToBail.Prisoner.Value = false
PlayerToBail.Team = game.Teams:FindFirstChild("Civilian")
createSystemMessage:FireAllClients((Player.Name .. ' has Bailed ' .. PlayerToBail.Name), Color3.fromRGB(0, 250, 0))
end
end
end)
And the local script which works:
script.Parent.AcceptButton.MouseButton1Click:Connect(function()
local PlayerName = script.Parent.TargetName.Text
game.ReplicatedStorage.Bail:FireServer(PlayerName)
print ("Bail Requested")
end)
I believe the problem occurs with the argument you're passing to the remote event.
In your client script you pass PlayerName as an argument. I'm assuming this is a string of the player's name:
game.ReplicatedStorage.Bail:FireServer(PlayerName)
PlayerName will actually be sent to the parameter "PlayerToBail", which I'm assuming is supposed to be a player object. Keep in mind that Roblox's RemoteEvents automatically pass in the player who fired the remote event as the first argument. So the "Player" parameter of the function connected to your remote event is the actual player object that has the local script that fired the remote event.
Instead, I would fire the remote event like this:
game.ReplicatedStorage.Bail:FireServer()
Since the player you want to bail is automatically passed as an argument, you don't need to add any additional arguments to FireServer. In addition, you would get rid of the "PlayerToBail" parameter in your server script.
Also note, it is unnecessary to have this line of code in your server script:
Player = game.Players:FindFirstChild(Player)
Player is already referring to an object in game.Players. In addition, Player is an object, not a string. So this would not work. You can simply use Player as is for your purposes.
More info on Remote Events: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
Please feel free to follow up if you still have issues.
When firing the server it automatically gives a parameter which is the player who sent it. So, you don't have to have the playerName part because after the player parameter is already there and you can get the name from that.

gmod GameMode Lua. IsPlayer retuning nill value

I'm trying to make a gmod gamemode. In my init.lua I wanted it so that way team members can't hurt each other. So I used this code
function GM:EntityTakeDamage( target, dmginfo )
if ( target:IsPlayer() and dmginfo:IsPlayer() ) then
if (dmginfo:Team() == target:Team()) then
dmginfo:ScaleDamage( 0.0 ) // Sets damage to 0
end
end
end
However it's giving me the error telling me that IsPlayer() is a nil value even though it should be returning a boolean. It points to no other lines other then the line with IsPlayer() and it's saying it is IsPlayer()
you have a typo in line 3. dminfo
You should narrow down which of your multiple IsPlayer() calls actually is nil
dmgInfo is a CTakeDamageInfo which has no function IsPlayer()
single line Lua comments are opened with --, not //
https://wiki.garrysmod.com/page/Category:CTakeDamageInfo
If you call a function and it says its nil, then check if it even exists. Or even better, check this befor you use the function in the first place.
And to prevent you from coming back in a minute, CTtakeDamageInfo also does not have a function Team() as well.
Check out CTDamageInfo:GetAttacker()

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