Roblox: How to kill player if they don't complete a task correctly? - roblox

I'm using a pre-made code door in which the player(s) must enter correct passwords to pass through. I would like the last door the local player must go through to kill them if they guess the password wrong. The door is a 2323 code door. It already came with a script. I've tried to edit the code for the incorrect password:
Input = ""
local player = game.Players.LocalPlayer
local character = player.Character
character.health = 0
print("Wrong Code")
However, I get a nil value "Character."
Any idea what I am doing wrong? Thanks.

You cannot access localplayer from a server script. Here is what you can do alternatively:
Make server script
Make a remote event
Fire the remote event with the password as the arg
Put this in your server script:
local remoteevent = -- reference remote event path here
local correctpass = 2323
remoteevent.OnServerEvent:Connect(function(p, pass)
-- p is player and pass is the password the user entered
if tonumber(pass) == correctpass then
-- User entered correct pass so open the door
else
p.Character.Humanoid.Health = 0
end
end)
And finally, fire the remote event from the local script just like this:
local remoteevent = -- reference remote event path here
local userinput = -- get the password the user entered here
remoteevent:FireServer(userinput)

Related

How to get PlayerGUI from a Server Script?

I am working on an inventory system and and now i need to make it save, but when trying to save it prints this in the output
PlayerGui is not a valid member of Player "Players.RidhoMBLR"
local function save(player)
local invTable = {}
for i, v in pairs(player.PlayerGui.ScreenGui.Inventory.InvFrame:GetChildren()) do
if v:IsA("TextButton") then
table.insert(invTable, v)
end
end
dataStore:SetAsync(player.UserId, invTable)
end
players.PlayerRemoving:Connect(save)
While you are accessing the path to the PlayerGui correctly, it is probably not replicated to the server (ie, it's existence is only observable on the client in LocalScripts).
So you will likely need to use a LocalScript to pull all of the relevant information out of the UI, and communicate it up to the server using a RemoteEvent. So move your code to a LocalScript :
local players = game.Players
local saveEvent = game.ReplicatedStorage.RemoteEvent -- find your RemoteEvent here
local function save(player)
-- only tell the server about it if we are the one leaving
if player ~= players.LocalPlayer then
return
end
-- get information about the inventory
local inventory = player.PlayerGui.ScreenGui.Inventory.InvFrame:GetChildren()
local invTable = {}
for i, v in ipairs(intentory) do
if v:IsA("TextButton") then
-- grab the text out of each button
table.insert(invTable, v.Text)
end
end
-- tell the server about our inventory
saveEvent:FireServer(invTable)
end
players.PlayerRemoving:Connect(save)
Then in a server Script, listen for when clients fire the event and save their data then :
local saveEvent = game.ReplicatedStorage.RemoteEvent -- find that same RemoteEvent
-- listen for when players report that they are leaving
saveEvent.OnServerEvent:Connect(function(player, inventoryTable)
-- save their information
dataStore:SetAsync(player.UserId, inventoryTable)
end)
So basically you can't access a player's PlayerGui through the server. PlayerGui is replicated locally for each client, hence in order to interact with a Player's Gui I'd recommend using RemoteEvents. By using Remote events you'd be sending a signal to the client which can then by picked up by a LocalScript. On the local script you can then fire another RemoteEvent back to the server with a list of all the items which you can then SetAsync on the server.

Roblox Studio - how do I run a script right before game starts?

I'm attempting to create a plugin that fetches additional code from a server before the user plays the game on roblox studio.
Basically, the user will use something like blockly to create luau code on a website and I want to sent that code to roblox studio. I've seen some plugins that fetch new data from a server from time to time and I've been able to do that, but I'd like to see if there's a way to only fetch the new code when the user clicks the play button because it could be expensive to request new data every 5 seconds or so.
Below is a simple plugin that attempts to send a request to the server when the game loads, but the script never goes beyond game.Loaded:Wait()
Main file:
local Request = require(script.Parent.Request)
local URL = "http://localhost:3333"
local toolbar = plugin:CreateToolbar("Test")
local button = toolbar:CreateButton("Test", "Test", "rbxassetid://4458901886")
local isListening = false
local request = Request.new()
local ok
local json
local function onClick ()
isListening = not isListening
if (isListening == false) then
return print("Not listening")
end
print("Listening")
if not game:IsLoaded() then
print(game.Loaded)
game.Loaded:Wait()
print("Game has started")
ok, json = request:Get(URL)
print(ok, json)
end
end
button.Click:Connect(onClick)
Request file:
local Request = {}
Request.__index = Request
function Request.new()
return setmetatable({}, Request)
end
function Request:Get(URL)
local ok, result = pcall(game.HttpService.GetAsync, game.HttpService, URL)
local json = game.HttpService:JSONDecode(result)
return ok, json
end
return Request
There isn't an explicit signal to detect when a game is about to start.
But, whenever you hit the Play button, the Edit session ends and the Play session begins. When a session ends, all of the plugins are unloaded. So you could use the plugin.Unloading signal to detect when the Edit session is ending, but it will also fire when the user closes the place, when you stop play testing, or when the plugin is disabled or uninstalled.
You could combine that signal with the RunService:IsEdit() function so that the behavior only triggers when exiting Edit mode, but this is still a really hazy signal.
So in a Script in your plugin, you could do something like this :
local RunService = game:GetService("RunService")
local Request = require(script.Parent.Request)
local URL = "<YOUR URL>"
-- listen for when sessions end
plugin.Unloading:Connect(function()
-- disregard sessions that aren't Edit Mode
if not RunService:IsEdit() end
return
end
print("Game about to start... maybe. The game might also be closing, or the plugin might be disabled from the PluginManager.")
local ok, json = request:Get(URL)
print(ok, json)
end)
Debugging this may be difficult as the Output console is cleared any time you start a Play session, so you won't see any of your print statements. But if you close the place, your logs will be preserved on the Welcome Screen. Just go View > Output to open the Output window.

Roblox - attempt to call a nil value

When trying to create a code, that on FireServer with Specific Player chosen a GUI would've been cloned into their PlayerGui, But instead I get a nil value.
Code:
local Blinder = game.ReplicatedStorage.RCD.Blind
Blinder.OnServerEvent:Connect(function(player, PlayerToBlind)
if not player:IsInGroup(7465879) then return false end;
script.BL:Clone().Parent = PlayerToBlind:WaitForChild("PlayerGui")
print("Done")
end)
Basically what I try to reach is if my Admin Panel Remoteevent is fired, and a Target has been chosen, the Targeted Player will become a Cloned GUI into their PlayerGui
Any fix on this error?
The error message itself is telling you that you are calling a function that doesn't exist. This issue is caused by an object not being the type you expect.
Unfortunately, the message is pointing at a line that has a few function calls, so it's difficult to say what is causing the exact error. Either script.BL isn't an object with a Clone() function, or PlayerToBlind isn't an object with a WaitForChild() function.
If you can break the operations into a few different lines and add safety checks along the way, you can guarantee that your code is safe and being called properly.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Blinder = ReplicatedStorage.RCD.Blind
local BL = script.BL
Blinder.OnServerEvent:Connect(function(player, PlayerToBlind)
-- check that the person calling Blinder:FireServer actually provided a PlayerToBlind
assert(type(PlayerToBind) == "string", "Blinder expects PlayerToBind to be the name of a player")
-- escape if the player is not in the appropriate group
if not player:IsInGroup(7465879) then
return
end
-- find the Player object based on the name provided from PlayerToBlind
local blindedPlayer = Players:FindFirstChild(PlayerToBlind)
if not blindedPlayer then
warn("Could not find player named : ", PlayerToBlind)
return
end
-- create a blinding UI on the other player
local targetGui = blindedPlayer:WaitForChild("PlayerGui")
local newBL = BL:Clone()
newBL.Parent = targetGui
print("Done")
end)
By adding asserts into your code, it won't stop your code from breaking, in fact it will force your code to break faster and in expected ways.
The issue has been fixed,
I have been setting up the LocalScript wrong.
(I grabbed the Text from a TextButton, Instead of the TextBox)

Different Script (SAP GUI) Codes for the same process in two different computers

I want to automate some operations for my works so I record a Script on SAP GUI(SAP LOGON PAD 720), then use it to create a excel macro and it works perfectly on my computer, but when I try to run it in other computers, it doesn't work at all and when I record the same process in other PC's the Script's code is different than that one which appeared in my PC. I want to know how this can be possible, how can I change the "code form" or "code language" on my PC so when I record a script it can be used on every computer.
This is the Script from my PC
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "vl03n"
session.findById("wnd[0]").sendVKey 0
'This is the script I recorded from my computer,in other pc this "LIKP-VBELN" doesnt appear
session.findById("wnd[0]/usr/ctxtLIKP-VBELN").text = "8105148724"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[7]").press
And this is if I record the same process from others PC's
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "vl03n"
session.findById("wnd[0]").sendVKey 0
'here is when the error begin "LIKP-VBELN" doesnt appear anymore so that is why the macro doesnt work
session.findById("wnd[0]/usr/ctxt").text = "8105148724"
session.findById("wnd[0]/usr/ctxt").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[7]").press
Check the LAN connection for the client and ensure its set to High Speed.
Regards,
ScriptMan

Roblox - Trying to get InvokeClient Remote Function Working

In my server script called MainGameScript I have this code block:
local myRemoteFunction = Instance.new("RemoteFunction")
myRemoteFunction.Parent = game.Workspace
myRemoteFunction.Name = "GetBuildInfo"
game.Players.PlayerAdded:connect(function(player)
print("[MainGameScript] added player")
for i,v in pairs(Regions) do
if (v.Value == 0) then
Regions[i].Value = player.UserId
break
end
end
local success, result = pcall(function() return myRemoteFunction:InvokeClient(player) end)
if success then
print(result)
else
print("Error calling RemoteFunction GetBuildInfo")
end
end)
I have a LocalScript in game.Workspace called LocalBuildScript which contains this:
function game.workspace.GetBuildInfo.OnClientInvoke()
print("[GetBuildInfo] will send back local build info")
return "this will be returned to the server caller script"
end
The GetBuildInfo function is never called. I have added some debug print lines to the calling function and the process seems to die at the point where it uses pcall... This is almost verbatim the Remote Function Client Invoke example from the Roblox Wiki, no idea why it is not working and am hoping somebody here has some experience using InvokeClient scripts.
Thanks,
Andy
The reason it isn't running is because LocalScripts being referenced by RemoteFunctions are expected to be inside of the Player instance somewhere in it. I'm fairly certain this is the case, as I've implemented a similar test system, but I held my LocalScript inside of the Player instance.