Why won't it remove my items from my Backpack - lua

I'm going to cut to the chase. Whenever this script executes:
local hotkey = Enum.KeyCode.P
local plr = game.Players.LocalPlayer
local char = plr.Character
local backpackItems = plr.Backpack:GetChildren()
local UIS = game:GetService("UserInputService")
local open = false
UIS.InputBegan:Connect(function(key, gp)
if key.KeyCode == hotkey then
char.UpperTorso.CFrame = CFrame.new(132.109, -84.7, 22.002)
local takenItemsStorageFolder = Instance.new("Folder")
takenItemsStorageFolder.Parent = game.ReplicatedStorage
takenItemsStorageFolder.Name = "Removed Items From ".. plr.Name
backpackItems.Parent = takenItemsStorageFolder
end
end)
It will teleport your player to a certain location on a map, but the lines involving variables "backpackItems", and "takenItemsStorageFolder" are trying to remove the tools/items in the player's Backpack. But the script won't complete this on execution.
Some other information:
This script is inside of a TextLabel in a ScreenGui.
(More info if requested)

I see two problems here:
You are creating a table with the backpack items when the script is first executed. Later when the "P" button is pressed you then want to move those objects to the takenItemsStorageFolder. But at that point there might be different items in the Backpack folder.
You are trying to parent the backpackItems table to the takenItemsStorageFolder object, but that does not work with a table. Instead you have to iterate through the actual objects (children of the Backpack) and parent each one.
So as a solution instead of
backpackItems.Parent = takenItemsStorageFolder
I would suggest you do:
for i,v in pairs(plr.Backpack:GetChildren()) do
v.Parent = takenItemsStorageFolder
end

Related

Why do keep getting this error, I'm trying to turn a character invisible when the function runs

local camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local startergui = game:GetService("StarterGui")
local char = Players.LocalPlayer.Character
local model = workspace.OceanVillagedr201["Wine Cellar"].WineDigitalkeypad
local screen = model.screen
local replicated_storage = game:GetService("ReplicatedStorage")
local CheckCode = Instance.new("RemoteEvent")
CheckCode.Name = "CheckWineCellarCodeEvent"
CheckCode.Parent = replicated_storage
local function Entercode(player)
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
game.StarterGui = replicated_storage.EnterWineCellarCode
end
screen.ProximityPrompt.Triggered:Connect(function(player)
Entercode()
end)
Im attempting to create a function that triggers when the proximity prompt is triggered, One piece of this Entercode() function is to toggle the playermodel's Transparency from 0 to 1 and Remove the Characters ability to move.
local function Entercode(player)
print("went")
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
But Im having trouble with this Piece. It keeps telling me "attempt to index nil" with anything dealing with trying to reference the Characters model. (FFC(), GetChildren(), Player.LocalPlayer.Character, etc.). I am using a local script because I plan to create a Remote Function for the result of EnterCode()
I believe your problem is that the character either was not spawned at the time of making char or is an old character (player respawned & a new character was made). A quick fix would be to redeclare char inside Entercode:
local function Entercode()
char = player.Character
char.Humanoid.RootPart.Anchored = true
for _, p in pairs(char:GetChildren()) do
if p:IsA("BasePart") then
p.Transparency = 1
end
end
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
end
There were also a number of other errors, here are a few changes I made:
local function Entercode()
In the original code, player was a parameter but was not sent as an argument Good thing you set the player variable at the beginning of the code.
if p:IsA("BasePart") then
p.Transparency = 1
end
In the original code, you didn't check to see if p was a part or not.
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
In the original code, you tried to set StarterGui to EnterWineCellarCode? I don't know what you were going for but I'm assuming you meant to parent EnterWineCellarCode to PlayerGui
Lastly, you might want to use GetDescendants() instead of GetChildren(). To better understand how the character works I recommend you read the wiki entry for it

IntValue changing but not shown

I'm scripting a lawn-mowing game in Roblox Studio and I've come across a problem. So every time I cut the grass(trawa) the IntValue goes up by 1. It works fine when it is assigned to a pick-up object and the points go straight to Player's Inventory folder:
local trawa = script.Parent
local function collect(otherPart)
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player then
local trawaType = trawa.Name
local trawaStat = player.Inventory:FindFirstChild(trawaType)
if trawaStat then
trawa:Destroy()
trawaStat.Value = trawaStat.Value + 1
end
end
end
trawa.Touched:Connect(collect)
So that script works fine, but that's not the way I want it to work. I want the points to be assigned directly after cutting grass, not after I collect a pick-up it drops. So I changed the script above so that the points are assigned to the tool's inventory and then are copied to player's inventory.
local trawa = script.Parent
local function onTouch(otherPart)
local tool = otherPart.Parent
if tool:IsA('Tool') and tool.Kosa.Value == true then
trawa.Parent = nil
local trawaType = trawa.Name
local trawaStat = tool.Inventory:FindFirstChild(trawaType)
trawaStat.Value = trawaStat.Value + 1
print(trawaStat.Value)
wait(3)
trawa.Parent = workspace
end
end
trawa.Touched:Connect(onTouch)
The value does indeed change because it goes up when I print it, but it's not changing in the properties and is not registered by other scripts, because it's not copied to player's inventory.
Then this is the script in ServerScriptService that should transfer points from the tool to player:
local starter = game:GetService("StarterPack")
local tool = starter:FindFirstChildWhichIsA('Tool')
local player = game:GetService("Players").LocalPlayer
function points(player)
player.Inventory.Trawa2.Value = player.Inventory.Trawa2.Value + tool.Inventory.Trawa2.Value
tool.Inventory.Trawa2.Value = 0
end
tool.Inventory.Trawa2.Changed:Connect(points)
Changing IntValue to NumberValue doesn't solve the problem.
Use a local script with a RemoteEvent that runs it in server script, local scripts are client side. So they don't save to the server, but if you run it on the server, it saves.
Click this link if you want to know more about RemoteEvents.
https://roblox.fandom.com/wiki/Class:RemoteEvent

Create phone and call friends in roblox studio

Hello I would like to know if someone knows how it would be possible to create a phone to call friends, for example you see the list of people connected on the phone and call one and when he answers you can have a private chat with him..
Any ideas or way to do this? I've looked everywhere and can't find a solution..
first, before we start making a chat function, we're going to need a list of players. Create a frame to be the phone, then add the following local script:
local frame = script.parent
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
for i,v in pairs(plrs) do
if v.Name ~= plr.Name then
local optionButton = Instance.new("TextButton")
optionButton.Text = v.Name
optionButton.Parent = frame
optionButton.MouseButton1Click:Connect(function()
--They chose a player to talk to
--We're going to create a value on the player so we can remember who they're talking to:
local val = Instance.new("StringValue")
val.Name = "chattingTo"
val.Value = v.Name
val.Parent = plr
end)
end
end
When they click on that button, we know they want to chat to the player V. You could add in some sort of ringing system so the other player can choose to accept/decline the call, but that isn't important. For the chat, to communicate between players, we're going to need a remove event (called "ChatEvent" under Replicated Storage) and a scrolling frame for the messages to show on.
Make a localscript under the chat frame, with:
local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("ChatEvent")
event.OnClientEvent:Connect(function(msg, from)
local textlabel = Instance.new("TextLabel")
textlabel.Text = from.Name..": "..msg
textlabel.Parent = script.Parent
end)
The script above will handle showing messages it receives. Now we want to be able to send messages, so add to the bottom of that localscript:
local inputBox = script.Parent.myTextBox --obviously change all variables to the route in your game
local sendButton = script.Parent.sendButton
local uis = game:GetService("UserInputService")
function sendChat()
local msgToSend = inputBox.Text
local sendTo = game.Players.LocalPlayer.chattingTo.Value
event:FireServer(sendTo, msgToSend)
local textlabel = Instance.new("TextLabel")
textlabel.Text = game.Players.LocalPlayer.Name..": "..msg
textlabel.Parent = script.Parent
end
sendButton.MouseButton1Click:Connect(sendChat)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Return then
sendChat()
end
end)
So now whenever we either click the send button or press our enter/return key, it will fire an event on the server. And when an event on the client is fired by the server, it will add a message to the chat. Now we need to just join them together with the following (server) script:
local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("ChatEvent")
event.OnServerEvent:Connect(function(from, to, msg)
--The variable 'to' is who we want to send it to - but it's a string! so:
local sendTo = game.Players[to]
event:FireClient(sendTo, msg, from)
end)
And that (should) be it. This would work, but it could face moderation action, as it doesn't use roblox's chat filter, so I recommend you integrate something with that.

Why doesn't this change gui code work for Roblox Studio?

I am trying to create a TextLabel that changes every 5 seconds. I have this code, but it doesn't work.
local player = game.Players:GetPlayerFromCharacter(part.Parent) —this is our gateway to getting the PlayerGui object.
local PlayerUI = player:WaitForChild(“PlayerGui”)
local txtLabel = PlayerUI[“Welcome_Text”].TextLabel
while x < 1 do
wait(5)
txtLabel.Text = “Welcome to The Shadow Realm!”
wait(5)
txtLabel.Text = “Warning: This game contains scenes that may be too scary for some roblox players”
end
I am getting an error message that says.
ServerScriptService.Script:2: attempt to index global 'part' (a nil value)
I don't know where to put my gui.
If I am correctly understanding what you are trying to do, you should be able to create a ScreenGui and place it in StartGui so that every player will have it copied to his PlayerGui when he joins the game. You could place a LocalScript inside of that GUI that would control the text on screen.
I see your LocalScript looking something like this:
-- Customize names on your own; these are just generic placeholders.
-- The structure of the GUI would look like this:
--
-- ScreenGui
-- LocalScript
-- Frame
-- TextLabel
local WELCOME = "Welcome to the Shadow Realm!"
local WARNING = "Warning! This game contains scenes that may be too scary for some players"
local runService = game:GetService("RunService")
local gui = script.Parent
local frame = gui.Frame
local label = frame.TextLabel
local function update_text()
label.Text = WELCOME
wait(5)
label.Text = WARNING
return
end
runService.RenderStepped:Connect(update_text)
Making this a LocalScript within a client-side GUI shifts the overhead to the client and eliminates the need to use the method Players::GetPlayerFromCharacter.

I am trying to change the Mesh texture of a tool in a players backpack

I made a part and added a script with a function that detects if the part was touched. If a player touches this part I need to access that players Backpack and a find a tool. In this case, it is a Pistol. I then try to change the TextureId of the Pistols mesh, which is inside of the Handle part within the Pistol
I have tried accessing the players backpack by using hit.Parent.Backpack,
but when I touch the part I get an error in the console saying
Backpack is not a valid member of Accessory
Here is the entire script...
function onTouched(hit)
local player = hit.Parent
local p = player.Backpack:FindFirstChild("Pistol")
local h = p:FindFirstChild("Handle")
local m = h:FindFirstChild("Mesh")
local id = m.TextureId
id = "rbxassetid://3707943717"
end
script.Parent.Touched:Connect(onTouched)
The expected result should be:
When a player touches this part it should look for a "Pistol" in the players Backpack and then look for the pistols "Mesh" and change the meshes "TextureId" to whatever texture I have set it to in the script.
I was able to fix this after some more research. My first mistake is that hit.Parent seems to only be the model. So i used that to find the "Humanoid" then from there got the name of the player so that i can find the player in the player list and access the backpack to change the pistols "Mesh.TextureId"
Here is the new script...
function onTouched(m)
local p = m.Parent:FindFirstChild("Humanoid")
if p ~= nil then
local n = p.Parent.Name
local player = game.Players:FindFirstChild(n, false) -- find the player that hit the Part.
local gun = player.Backpack:FindFirstChild("Pistol")
if player == nil then return end -- escape if something goes wrong.
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh
mesh.TextureId = "rbxassetid://3707943717" -- change the gun texture
end
end
script.Parent.Touched:Connect(onTouched)

Resources