Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I'm trying to make it so you can only do an ability when you are holding an item, but it won't work?

Ok, so I am trying to do an ability where you can stop time only when you are holding a sword. So, here is the problem. It works fine the first time before I pull out my sword, when you press the button, it will only do it when you are holding the sword. But after, even when you aren't holding the sword, it still stops time! Does anyone have any ideas what i need to do? Thank you.

Also, i have tried wrapping it in a loop, but that just crashes it. (also, this isn't required but I'm wondering how i could put an animation to play when you do it. Thanks!

Here is the script: (It's a local script inside the tool)

local UIS = game:GetService("UserInputService")
local tool = script.Parent
local player = game.Players.LocalPlayer
local enabled = false
local remote = game:GetService("ReplicatedStorage"):FindFirstChild("TimeStop")


tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.E then
			if enabled == false then
				enabled = true
				remote:FireServer("TimeStop")
				for i=70, 100,5 do
					wait()
					workspace.Camera.FieldOfView = i
				end
				for i=100, 70,-5 do
					wait()
					workspace.Camera.FieldOfView = i
				end
				wait(55)
				enabled = false
			end
		end
	end)
	end)

2 answers

✓ Accepted answer

Try rewriting your code just a bit like this.

local UIS = game:GetService("UserInputService")
local tool = script.Parent
local player = game.Players.LocalPlayer
local enabled = false
local remote = game:GetService("ReplicatedStorage"):FindFirstChild("TimeStop")


tool.Equipped:Connect(function()
	enabled = true
 
end)
tool.Unequipped:Connect(function()
	enabled = false
	-- You can write a code for the time to go on here
end)
UIS.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.E then
            if enabled == true then
                remote:FireServer("TimeStop")
                for i=70, 100,5 do
                    wait()
                    workspace.Camera.FieldOfView = i
                end
                for i=100, 70,-5 do
                    wait()
                    workspace.Camera.FieldOfView = i
                end
               
            end
        end
end)

And btw, why do you need the 'wait(55)' part.

4 comments

0

use "task.wait()" if pro, performance difference is very noticeable, create 1000 small parts and move each of them by a bit every "wait()", then try the same with "task.wait()"

0

Thank you, I will see if this works! The reason I used the wait() was because I was trying to make a cooldown.

0

Yep, it works! Thank you so much!

0

If you wanna make a cooldown, you can add the wait(55) after you put enabled = false. After that, you can add enabled = true after wait(55). The cooldown will be 55 seconds.

Im not really advance with scripting but maybe Instead use a GUI button that will activate It

1 comment

0

No, you can use a tool.

This discussion is preserved as it was. New answers, comments, and votes are closed.