Make an event happen automatically

So basically, I have a textbutton, which when pressed unanchors something, so like this

local isUnAnchored = false --variable to determine if its false

TextButton.MouseButton1Click:Connect(function()
    Part.Anchored = false
    isUnAnchored = true
end)

print("something")

And what i want is that if it isnt clicked in like 30 seconds that it should unanchor it automatically, i came up with this:

wait(30)
if isUnAnchored == false then
     isUnAnchored = true
end

print("something")

The issue with this is that if the player does click it, its still going to wait 30 seconds and only then print it, so i need a solution for this

When does the 30 seconds start though?

Here I think this is better

local isUnAnchored = false --variable to determine if its false

TextButton.MouseButton1Click:Connect(function()
    Part.Anchored = false
    isUnAnchored = true
end)

wait(30)
if isUnAnchored == false then
     isUnAnchored = true
end

print("something")

The easiest way to do this would to probably just to wrap the wait into a coroutine, so that you can have both threads run at the same time.

Probably something like this:

local isUnAnchored = false --variable to determine if its false

task.spawn(function () 

  wait(30)
    if isUnAnchored == false then
        isUnAnchored = true
    end

print("something")

end) 

TextButton.MouseButton1Click:Connect(function()
    Part.Anchored = false
    isUnAnchored = true
end)

Here is the code I believe you are looking for

local isUnAnchored = false

local function unAnchor()
	Part.Anchored = false
	isUnAnchored = true
end

TextButton.MouseButton1Click:Connect(unAnchor)

task.wait(30)
unAnchor()

The issue with this code is that it needs to be on the client, which means the part that gets unanchored sort of gets bugged out in a state of it being unanchored, allowed to be pushed around but floating (on the clients viewpoint)
To combat this you will need to send a remote event to the server where you will unanchor it there

Yeah but then you will have to secure the RemoteEvent or exploiters can literally unanchor all the parts in workspace.

Also here you forgot to add the Part argument

Only if he defines and sends the part from the client to the server, which he shouldnt be doing
Of course there would be security issues with it, that would be up to him to do, Im only providing him assistance on what he should do

He never stated he wanted chosen parts to be unanchor, only one part, predefined part

1 Like

I hope this is what you want.
This created a RemoteEvent that when fired unanchors a Part if it’ s in the BaseParts table.

Script in ServerScriptService:

local BaseParts = { workspace.Part }

Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")).OnServerEvent:Connect(function(Player, BasePart)
if table.find(BaseParts, BasePart) then
BasePart.Anchored = false
end
end)

LocalScript:

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

local function Unanchor()
RemoteEvent:FireServer(Part)
end

TextButton.Activated:Connect(Unanchor)

task.spawn(function()
task.wait(30)
Unanchor()
end)

I explained this very badly,

So basically it should print(“something”) either if the button was clicked or the 30 seconds have passed

if i press the button it should instantly print it

and if i don’t press it in 30 secs then it should print then

I will point you to my post which already solved this. I’m not sure why this topic is still ongoing.

1 Like

i tried this and it prints when the 15 seconds end but not when the button is pressed

Just put a print inside of here.

TextButton.MouseButton1Click:Connect(function()
    Part.Anchored = false
    isUnAnchored = true
    print("something") --here
end)

Also, please try debugging stuff yourself instead of immediately going to the forum to report an issue! I promise it will help you learn a lot more if you try finding the problem yourself.

just use task.delay() then?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.