I have made this code; what I want is so when the part is clicked, it does the function fadeWaitShow, but it does not seem to be working. I tried putting it in a loop but it would just do the function again and again by itself. Help, if you can and thanks! (Code is below)
local click = script.Parent.ClickDetector
local function fade()
script.Parent.CanCollide = false
script.Parent.Transparency = 0.5
end
local function show()
script.Parent.CanCollide = true
script.Parent.Transparency = 0
end
local function fadeWaitShow ()
fade()
wait(1)
show()
end
click.MouseClick:Connect(fadeWaitShow())
Thanks!
In your MouseClick connection, you are calling the function instead of passing the function handle to the connection. This results in the code evaluating the line as click.MouseClick:Connect().
So to fix this, don't call the function.
click.MouseClick:Connect(fadeWaitShow)
Related
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
while true do
wait()
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local d = b[c]
d:Play()
wait(d.TimeLength)
mutebutton.MouseButton1Click:Connect(function()
d.Volume = 0
end)
end
If i was to replace d.Volume = 0 with print(d.Volume) or print("testing") then it will work however when I change it to actually mute the audio, it dosent want to work. Anyone know why?
If you want to stop playing a Sound, I would suggest using the :Pause() method, like so:
mutebutton.MouseButton1Click:Connect(function()
d:Pause()
end)
The main problem with your script is that the MouseButton1Click:Connect (listening for clicks) is done only after the wait(d.TimeLength) (after the song has finished playing).
Adding on from ivy's suggestion of using d:Pause(), I would suggest doing something like this:
while true do
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local d = b[math.random(1, #b)]
d:Play()
local connection = mutebutton.MouseButton1Click:Connect(function()
if d.IsPaused then -- Makes the pause button a play button if the song is paused already
d:Resume()
else
d:Pause()
end
end)
d.Ended:Wait() -- Wait until music ends
connection:Disconnect() -- Remove connection (to prevent multiple functions on the same button click)
end
local colorwheel = script.Parent
local clickdetector = colorwheel.ClickDetector
local barlight = workspace.barlight:GetChildren()
--attempting to establish function as variable--
local rightmouse = function onMouseClick()
print(" turned the lights off")
barlight.Transparency = 1
end
local leftmouse = function onMouseClick()
print(" turned the lights on")
barlight.Transparency = 0
end
clickdetector.RightMouseClick:connect(rightmouse)
clickdetector.MouseClick:connect(leftmouse)
Im trying to create a function so that when the model "colorwheel" is clicked, it changes the transparent property of the "barlight" model. I want to establish two separate variables for the onMouseClick() function so I can change the way the function behaves based on what mouse button clicked it. One to turn the light it on and one to turn it off. All of this is being done within a server script rather than a local one (not sure if that means anything)
local rightmouse = function onMouseClick()
local leftmouse = function onMouseClick()
When I try to make onMouseclick() a variable, it gets a red underline right under the word itself, and tells me "Workspace.barlight wheel.Script:5: Expected '(' when parsing function, got 'onMouseClick'" Any ideas why?
function onMouseClick() end defines a function. It does not resolve to a function value. Hence you cannot assign it to a local variable as attempted in
local rightmouse = function onMouseClick()
print(" turned the lights off")
barlight.Transparency = 1
end
This is incorrect syntax. You can define a function two ways:
local function myFunction() end
or
local myFunction = function () end
So either
local rightmouse = function ()
print(" turned the lights off")
barlight.Transparency = 1
end
If you want the function's name to be rightmouse, or
local function onMouseClick()
print(" turned the lights off")
barlight.Transparency = 1
end
if you want the functions name to be onMouseClick
Background:
Bear minimal objects. Just a player and a part (rectangular prism) that's floating in the air and anchored.
I honestly don't understand what is happening under the hood so its hard to figure out. Without debounce, upon event firing, the callback function is invoked by connect() or the event handler (not sure), and, without debouncing, the function is invoked multiple times as print statements are repeated on the output box. So, with a variable (of type boolean) that stores a debounce flag, that solves it. I then try to "un-debounce" when the player's model is outside out of the box's model. But, I don't know how to do that properly.
Here's my code attempt:
local box = game.Workspace.BoxModel;
local debounce = false;
local function onTouchedDebounced()
if (debounce == false)
then
debounce = true;
print("Hello! onTouchedDebounced() has run.");
box.Touched:Connect(onTouchedDebounced));
end
end
local function onTouchedUndebounced()
if (debounce == true)
then
print("Hello! onTouchedUndebounced() has run.");
debounce = false;
end
end
box.Touched:Connect(onTouchedDebounced);
box.TouchEnded:Connect(onTouchedUndebounced);
The heart of what you're doing is sound : start blocking after the first event, and unblock some time later. If this were with button presses or mouse-clicks, your solution would work fine. This is complicated by the Touched event, as it fires with any part that touches it, and a player's character could have multiple touch points.
The Touched and TouchEndeded events give you a reference to the instance that has touched the part.
If the goal is to only fire the box event once per player or once while anyone is touching it, you can keep a dictionary of parts that are currently touching the box. When a part touches, you increment a counter. When a part stops, you decrement it. You only remove the debounced flag once all touch points have been removed.
local box = game.Workspace.BoxModel
local playersTouching = {} --<string playerName, int totalParts>
local function onTouchedDebounced(otherPart)
-- check that the thing that touched is a player
local playerModel = otherPart.Parent
if not playerModel:IsA("Model") then
warn(otherPart.Name .. " isn't a child of a character. Exiting")
return
end
-- check whether this player is already touching the box
local playerName = playerModel.Name
local total = playersTouching[playerName]
if total and total > 0 then
warn(playerName .. " is already touching the box")
playersTouching[playerName] = total + 1
return
end
-- handle a new player touching the box
playersTouching[playerName] = 1
-- Do a thing here that you only want to happen once per event...
print(string.format("Hello! onTouchedDebounced() has ran with %s and %s", playerName, otherPart.Name))
end
local function onTouchedUndebounced(otherPart)
-- decrement the counter for this player
local playerName = otherPart.Parent.Name
if playersTouching[playerName] == nil then
return
end
local newTotal = playersTouching[playerName] - 1
playersTouching[playerName] = newTotal
-- if the total is back down to zero, clear the debounce flag
if newTotal == 0 then
playersTouching[playerName] = nil
print(string.format("Hello! onTouchedUndebounced() has ran and %s has no more touching parts", playerName))
end
end
box.Touched:Connect(onTouchedDebounced)
box.TouchEnded:Connect(onTouchedUndebounced)
Problem
So currently, I am making a game in Roblox. I am working on tweening one of my GUIs, but the code isn't changing the var I am working with called state. The state var is supposed to tell if it's open or closed (State would = true if open, else, State would = false).
I have tried to make the variable a local var. But still the same output. I checked the output by printing the state var. Which would always be the same as the default value.
Code
-- Local Variables
local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
State = false
local Button = script.Parent.Button
-- Open/Close Statements
if State == true then
Button.Text = 'Close!'
script.Parent.Button.MouseButton1Click:connect(function()
Frame:TweenPosition(UDim2.new(0.3,0,1.2,0))
State = false
end)
end
if State == false then
Button.Text = 'Open!'
script.Parent.Button.MouseButton1Click:connect(function()
Frame:TweenPosition(UDim2.new(0.305,0,0.25,0,'Bounce',1.5))
State = true
end)
end
I expect the output of the code to set the var state to be True when open and False when closed.
You need to be careful about how you connect your Mouse1Click event listeners.
When you read your script from top to bottom, you'll see that since State starts as false, the only listener you've connected is the second one. This means that when you click on the button, it will only ever tween the Frame into the Open state. It is better to write one click handler that handles this logic on every click.
local Button = script.Parent.Button
local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
State = false
Button.MouseButton1Click:connect(function()
if State == true then
Button.Text = 'Close!'
Frame:TweenPosition(UDim2.new(0.3,0,1.2,0))
else
Button.Text = 'Open!'
Frame:TweenPosition(UDim2.new(0.305,0,0.25,0,'Bounce',1.5))
end)
State = not State
end)
You are missing State = false after Frame:TweenPosition(UDim2.new(0.3,0,1.2,0)) line. You never switch its value back to false after it was changed to true.
Say I am using timer in my game for example
timer.performWithDelay(1000, function() end, 1)
Is it necessary that I should assign this to an local variable and cancel after its use i.e,
local timerVar = timer.performWithDelay(1000, function() end, 1)
timer.cancel(timerVar)
timerVar = nil
Help me with this .....
If you don't expect the timer to be cancelled, you don't have to store it's reference. But I also recommend to not use anonymous function in timer. It's harder to debug then, because you'll not know the name of a function if it's throw an error.
the cancel() method is for stopping and removing the timer before it finishes. There's no need to call it after the timer is done.
i think you try this below.
local timerVar = timer.performWithDelay(1000, function(e)
timer.cancel(e.source)
e.source = nil
end, 1)