Roblox Lua Values not changing - lua

I've added multiple different NumberValues and BoolValues, yet when i try to change the values with something like this for example:
local i = 1
for i == 1
game.Workspace.Time.Value = 0
wait(120)
game.Workspace.Time.Value + 0.5
end
and the NumberValue in the workspace won't change

Random side note: game.Workspace is deprecated, use 'workspace' instead.
Also, the syntax behind it is all wrong, which is an honest mistake. It should look like this:
-- Assuming "Time" is a 'NumberValue' under workspace
-- Assuming this script is in workspace
local i = 1
while (i == 1) do
local time = workspace:FindFirstChild("Time") -- Usage of the 'FindFirstChild' method
time.Value = time.Value + 0.5
wait(120)
end
However, this itself is bad practice because this will yield whatever thread you're running this in, and for this I suggest coroutines!
local function addTime()
local varContainer = workspace:GetFirstChild("Time")
repeat
varContainer.Value = varContainer.Value + 0.5
wait(120)
until false
end
local newThread = coroutine.create(addTime) -- Create the new coroutine
coroutine.resume(newThread) -- Run it forever in another running thread

Related

how should I fix While True loop repeating and stopping unwantedly (Roblox)

I am trying to make a part duplicate then delete the duplication script with the new part without deleting the script in the old part, this is in Roblox studio.
This is my code
local block = script.Parent
while true do
local xrange = math.random(-250,250)
local zrange = math.random(-250,250)
local item = block:Clone()
item.Parent = game.Workspace
item.Position = Vector3.new(xrange,.5,zrange)
item["Block Script"]:Destroy()
game.ReplicatedStorage.ItemCount.Value = game.ReplicatedStorage.ItemCount.Value + 1
wait(1)
end
The problem is that it runs as if it is supposed to duplicate 3 times for every loop and rather than creating 1 new part each second, it creates 3 parts. The main issue is that sometimes 1 of the 3 new parts won't have the other script that I have inside the parts, making the part a useless part that causes problems.
Does anyone see a problem?
You can also comment if you want more clarification.
If this is just an error or cannot be solved through the information given, I can probably work around but please comment if you need my clarification
Its duplicating 3 times because you are destroying the script last.. what you should try doing is:
1- creating a new folder on workspace ( going to call it "blocks" for example )
2- making the block and script of the block as the children of the "blocks" folder
3- and placing this code on the script:
local block = game.workspace."**blocks**".(Your part name)
while true do
local xrange = math.random(-250,250)
local zrange = math.random(-250,250)
local item = block:Clone()
item.Parent = game.Workspace
item.Position = Vector3.new(xrange,.5,zrange)
game.ReplicatedStorage.ItemCount.Value = game.ReplicatedStorage.ItemCount.Value + 1
wait(1)
end
If this does not work you can reply to this comment.

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

The block is not being cloned and no error message is showing.(Roblox Studio)

I am making a placing system (it is basic so far) but it is not placing anything. the code works up until the 'while whenclicked = true' part, here's the code:
print('works')
while true do
print('works2')
local ImportedValueX = game.ReplicatedStorage.ActPosX
local ImportedValueY = game.ReplicatedStorage.ActPosY
local ImportedValueZ = game.ReplicatedStorage.ActPosZ
local Block = game.ReplicatedStorage.Experimental
local WhenClicked = game.ReplicatedStorage.WhenClicked.Value
print('works3')
while WhenClicked == true do
print('wore')
PlacedBlock = Block:Clone()
PlacedBlock.Parent = workspace
PlacedBlock.Position.X = ImportedValueX
PlacedBlock.Position.Y = ImportedValueY
PlacedBlock.Position.Z = ImportedValueZ
WhenClicked = false
wait(1)
end
wait(0.1)
end
The variables work fine and the whenclick part also works, i think the while whenclicked part is broken.
I see a problem here:
PlacedBlock.Position.X = ImportedValueX
PlacedBlock.Position.Y = ImportedValueY
PlacedBlock.Position.Z = ImportedValueZ
The X, Y, Z are read-only properties. You need to populate them by creating a new Vector3 object and assigning it to the Position property, like this:
PlacedBlock.Position = Vector3.new(ImportedValueX, ImportedValueY, ImportedValueZ)
Updated:
I am making the assumption that you are trying to use replicate storage to signal the mouse click state (whenClicked) from your client to the server. The server then checks on the state as well as the x/y/z position in a loop. This is not working, because ReplicatedStorage does not replicate your values to the server. That would probably be an opening for exploits otherwise.
So, in order to signal something from your client to your server you should use RemoteEvent or RemoteFunction (look those up in the reference manual). In your case, your server script could look something like this:
local event = Instance.new("RemoteEvent", game.ReplicatedStorage)
event.Name = "MyRemoteEvent"
local Block = game.ReplicatedStorage.Experimental
event.OnServerEvent:Connect(function(plr, position, whenClicked)
if whenClicked then
print('wore')
local placedBlock = Block:Clone()
placedBlock.Parent = workspace
placedBlock.Position = position
end
end)
So this would create a remote event in ReplicatedStorage and then listen to it. When it is called from the client, it will then do what you wanted to do (clone the part and position it).
In your client script you would trigger the event like this:
-- wait until the server has created the remote event
local event = game.ReplicatedStorage:WaitForChild("MyRemoteEvent")
-- do whatever you need to do, then call trigger the event:
event:FireServer(Vector3.new(5,5,5), true)

Awesome WM (v3.5.5) keygrabber alternative

I've never liked the default window switching possibilities in Awesome, so I thought I'd implement Alt-Tab behavior that takes history into account (and does fancy opacity effects).
When Alt-Tab is pressed, the entire history is recorded in a table, and appended to that history are the minimized windows (within the same tag). When this table is generated, I instantiate a keygrabber that captures Tab-press events (to switch to the next client in the table) and Alt-release events (to abort entirely).
A flag keeps track of whether the user is in the process of Alt-tabbing, to prevent the table from being generated over and over again.
The code (it's a lot and you probably don't need to see it, but my experience tells me that when I don't post all the code, people will ask for it eventually):
altTabbing = false
altTabIndex = 1
altTabHistory = {}
clientOpacities = {}
function altTabSetOpacities(restore)
for i,c in pairs(altTabHistory) do
if not restore and i ~= altTabIndex then
c.opacity = 0.5
else
c.opacity = clientOpacities[i]
end
end
end
function myAltTab()
-- First check if the user is already alttabbing, in which case the history
-- should NOT be updated. If the user has just pressed alt-tab, generate a new
-- history-table
if not altTabbing then -- generate history-table
-- Clear Tables
for i in pairs(altTabHistory) do altTabHistory[i] = nil end
for i in pairs(clientOpacities) do clientOpacities[i] = nil end
-- Get focus history for current tag
local s = mouse.screen;
local idx = 0
local c = awful.client.focus.history.get(s, idx)
while c do
table.insert(altTabHistory, c)
table.insert(clientOpacities, c.opacity)
idx = idx + 1
c = awful.client.focus.history.get(s, idx)
end
-- Minimized clients will not appear in the focus history
-- Find them by cycling through all clients, and adding them to the list
-- if not already there.
-- This will preserve the history AND enable you to focus on minimized clients
local t = awful.tag.selected(s)
local all = client.get(s)
for i = 1, #all do
local c = all[i]
local ctags = c:tags();
-- check if the client is on the current tag
local isCurrentTag = false
for j = 1, #ctags do
if t == ctags[j] then
isCurrentTag = true
break
end
end
if isCurrentTag then
-- check if client is already in the history
-- if not, add it
local addToHistory = true
for k = 1, #altTabHistory do
if altTabHistory[k] == c then
addToHistory = false
break
end
end
if addToHistory then
table.insert(altTabHistory, c)
table.insert(clientOpacities, c.opacity)
end
end
end
-- reset current index and flag
altTabIndex = 1
altTabbing = true
-- Now that we have collected all windows, we should run a keygrabber
-- as long as the user is alt-tabbing:
keygrabber.run(
function (mod, key, event)
-- Stop alt-tabbing when the alt-key is released
if key == "Alt_L" and event == "release" then
altTabbing = false
altTabSetOpacities(true)
c = altTabHistory[altTabIndex]
client.focus = c
c:raise()
return false -- stop keygrabber
end
-- Move to next client on each Tab-press
if key == "Tab" and event == "press" then
myAltTab()
return true -- keep going
end
return true -- keep going
end
)
end -- if not altTabbing
-- at this point, the user is alt-tabbing, so we should raise
-- the next client in the history-table
if #altTabHistory < 2 then return end
-- Switch to next client
altTabIndex = altTabIndex + 1
if altTabIndex > #altTabHistory then
altTabIndex = 1 -- wrap around
end
-- focus on current client
local c = altTabHistory[altTabIndex]
c.minimized = false
c:raise()
-- make current client stand out
altTabSetOpacities(false)
end
I realize there's a lot of code, but the main thing is the keygrabber. For still unknown reasons, Awesome sometimes crashes while I'm Alt-Tabbing using this approach. I want to replace the keygrabber by connecting signals to the Alt and Tab keys, and disconnecting them as soon as the user is done. However, I'm not able to do this for some reason.
I instantiate a new key-object like this:
local altkey = awful.key({}, "Alt_L")[1]
I found out by trial and error that awful.key() actually returns a table of which I could query the first element for key, keysym etc, hence the [1]. However, when I try to connect a signal to this object, the LUA interpreter complains and tells me it's a nil object. So my question is: am I doing the right thing here? Is it even possible to replace the keygrabber in the way I intend to?
To use the Alt_L key in Awesome you should refer to "Mod1" in your rc.lua file, to make it mor readable I added the following line to the beginning of my configuration so Alt_L can be used.
Alt_L = "Mod1"

Resources