Roblox GUI pop-up and close with press of key 'E' - lua

I'm a new developer to roblox and figured it would be a good place to start learning to make games. I was working on my game today and I ran into an issue. I wanted to press 'E' while by a NPC and I got that to work. The only problem was, I have no clue how to make the GUI appear and Dissapear when I press E. This is what I have written,
local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Npc = game.Workspace.Noob.Head
UIS.InputBegan:connect(function(keyCode)
if keyCode.keyCode == Enum.KeyCode.E then
if (Npc.Position - HumanoidRootPart.Position).magnitude < 15 then
game.StarterGui.TextF.TextLabel.Visible = 0
wait(3)
game.StarterGui.TextF.TextLabel.Visible = 1
end
end
end)
it comes out with no error. The code to press E still works, but the GUI does not pop up. I have tried setting the values to True or False

I've Figured it out. if anyone is looking at this question and wondering, this script is almost ok. The only problem is that you cannot change a GUI from the starter GUI, you have to call it from LocalPlayerGui Like this:
local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Npc = game.Workspace.Noob.Head
UIS.InputBegan:connect(function(keyCode)
if keyCode.keyCode == Enum.KeyCode.E then
if (Npc.Position - HumanoidRootPart.Position).magnitude < 15 then
print("Key E was pressed")
game.Players.LocalPlayer.PlayerGui.TextF.TextLabel.Visible = true
wait(3)
game.Players.LocalPlayer.PlayerGui.TextF.TextLabel.Visible = false
end
end
end)

Related

The door data is not saved in Roblox

I wrote a door save system.
That is, if the user previously bought them, then when re-entering the game, they must be open.
My code works, but the door doesn't save at all.
-- DoorsDataStore
-- Save Stats Doors
local opend = false
local datastorage = game:GetService("DataStoreService")
local isitopen_1 = datastorage:GetDataStore("Door")
game.Players.PlayerAdded:Connect(function(player)
local boolValueDoors = Instance.new("Folder")
boolValueDoors.Name = "BoolValueDoors"
boolValueDoors.Parent = player
local door_1 = Instance.new("BoolValue")
door_1.Parent = boolValueDoors
door_1.Name = "BoolValueDoor_1"
door_1.Value = isitopen_1:GetAsync(player.UserId)
print("True or False")
print(player.BoolValueDoor_1.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, erromsg = pcall(function()
isitopen_1:SetAsync(player.UserId, player.BoolValueDoor_1.Value)
end)
if erromsg then
warn("Error")
end
end)
-- TouchDoor
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if (humanoid ~= nil) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Coins.Value >= script.Parent.Price.Value then
player.leaderstats.Coins.Value -= script.Parent.Price.Value
player.leaderstats.Level.Value += 1
script.Parent:Destroy()
player.BoolValueDoors.BoolValueDoor_1.Value = true
print("Save Door")
end
end
end)
I tried writing this code in different ways, in different versions, tried through validation. My code still doesn't do what I want.
There are multiple possible problems I see:
1.
SetAsync takes a string as the first argument, you are giving it a number value. To fix this use tostring(player.UserId)
2.
When the player first joins, the value will be set to nil, because there is no data in the datastore under the UserId, and nil is not a boolean.
I’m not sure if these are actual issues and I currently cannot check if these are real problems because I’m not on my computer but they may be something you want to change.
Also it would be nice to know if you encountered any errors when executing the code.
You should also make trigger a remote event that opens the door on the clientside in the PlayerAdded function if the value is true.

script works but nothing happens in-game in roblox studio

I am having problems with the functionality of a script and I don't know how to solve it.
In the images below attach photos of the in-game problem, this means that the whole script works and in-game the property of the visible "bframe" is false but in-game this is not shown and I want to know how I can solve it ..
in-game mode the visibility of the "Bframe" it becomes false in the property (that's fine) but in-game this is not seen..
Does anyone know how to solve it?
Button script
-- Events
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Evento = ReplicatedStorage.RemoteEvent
-- end of Events
local button = script.Parent
local frame = script.Parent.Parent.Parent.Frame
Evento.OnClientEvent:Connect(function(argumento1)
button.MouseButton1Click:Connect(function()
if frame.Visible == false then
frame.Visible = true
button.Visible = false
else
frame.Visible = false
end
end)
end)
Server script
-- Events
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Evento = ReplicatedStorage.RemoteEvent
-- end of Events
game.Players.PlayerAdded:Connect(function(player)
Evento:FireClient(player,"argumento1")
print("testing probado")
end)
Redteam / touch part script called script
local wll = script.Parent.Part
local fr = game.StarterGui.TeamChangerGui.BFrame
local function Toch()
fr.Visible = false
fr.button.Visible = false
print("visible desactivado")
end
wll.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Toch()
end
end)
**images**
enter image description here
enter image description here
enter image description here
Is the button script a localscript? if it is then it may be to do with the fireclient although I am not good at it and I usually use fireallclients passing through the player name then checking if its the correct name on the clients side script.
Otherwise I am not sure as it is an interesting issue i have never encountered. Hope it works!

I want to code it so that when I open chat in roblox and press something that has the letter "r" in the word, i don't reset

can someone help me with that? I already got the code for resetting but when i open chat and say rrrrrrr, i keep dying.
here it is:
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local enabled = true
local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.R and enabled then
character.Head:Remove()
enabled = false
wait(6)
enabled = true
end
end)
UserInputService#inputBegan gives you gameProcessedEvent as the second parameter of the function.
It indicates whether the game engine internally observed this input and acted on it. Generally, this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent would be true. This applies to TextBoxes, such as the chat.
All you need to check if if gameProcessedEvent is true.
local UserInputService = game:GetService("UserInputService");
local PlayersService = game:GetService("Players");
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return false end
if (input.KeyCode == Enum.KeyCode.R) then
PlayersService.LocalPlayer.Character.Head:Destroy()
end
end)

ReplicatedFirst:RemoveDefaultLoadingScreen() not firing in Studio

I'm back with yet another problem. I'm trying to make a custom loading screen for my game, but RemoveDefaultLoadingScreen doesn't seem to be firing. Can anyone help me with this? Here's my code:
local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
ReplicatedFirst:RemoveDefaultLoadingScreen()
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
PlayerGui:SetTopbarTransparency(1)
local Loading = ReplicatedFirst.Load
local ScreenGui = Instance.new("ScreenGui")
ScreenGui.IgnoreGuiInset=true
local Bar = Loading.Frame.TextLabel
ScreenGui.Name = "LoadScreen"
Loading.Parent = ScreenGui
ScreenGui.Parent = PlayerGui
local Info = TweenInfo.new(0.7,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,true,0)
local Tween = TweenService:Create(Bar,Info,{Position=UDim2.new(0.8,0,0.6,0)})
spawn(function()
while true do
Tween:Play()
Tween.Completed:Wait()
end
end)
if not game:IsLoaded() then
game.Loaded:Wait()
end
for i=0,1,0.1 do
Loading.BackgroundTransparency=i
Bar.TextTransparency=i
Loading.Frame.Load.TextTransparency=i
wait()
end
ScreenGui:Destroy()
Everything works fine, except that the third line doesn't work in Studio. Is it supposed to be this way? Help would be greatly appreciated.
I tested it, it works just fine in Roblox Studio. Note this, though:
In studio the load screen goes away almost instantly, so you won't see much of a difference.
I'd put a wait(5) in your script before the if not game:IsLoaded() then line, that will simulate a 5 second load. Then you can play with having vs not having the ReplicatedFirst:RemoveDefaultLoadingScreen() command present, and you'll see the difference.

Roblox shop not showing

This is for my game. I have all the code right with no errors.
script.Parent.MouseButton1Click:Connect(function()
game.StarterGui.ScreenGui.Enabled = true
script.Parent.Visible = false
end)
But when I start the game the code wont work properly, only the shop button disappears which is: "script.Parent.Visible = false" and the show me the shop: "game.StarterGui.ScreenGui.Enabled = true" is not working,and wont show up the shop, yes I have the ScreenGui disabled, and there are no output errors.
you need to make sure that you are useing the local StarterGui
when you do game.StarterGui.ScreenGui.Enabled = true you are changing the global instance and not the local. To fix this you need to use script.Parent and not game.StarterGui.
Below is some example code:
script.Parent.MouseButton1Click:Connect(function()
script.Parent.%how ever many parents it takes go get back to StarterGui%.ScreenGui.Enabled = true
script.Parent.Visible = false
end)
In the second line, You are enabling a GUI that's located in the StarterGui not the PlayerGui, To fix the problem, Change your code to this:
script.Parent.MouseButton1Click:Connect(function()
game.Players.LocalPlayer.PlayerGui.ScreenGui.Enabled = true
script.Parent.Visible = false
end)
Also, when posting code next time, use {} button instead

Resources