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
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)
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
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
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)
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.