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

How do I make sure the code inside of a function fires if a BoolValue's value is true?

I'm making an Axe for my previous questions about the tree-chopping issue. But instead, I want the code inside of the tool to fire when the BoolValue called "ActivatedTool". Here's my scripts:

Local Script (Inside tool)

repeat wait() until game.Players.LocalPlayer.Character
local Humanoid = game.Players.LocalPlayer.Character.Humanoid

local Animation = Humanoid:LoadAnimation(script.Parent.ChopAnimation)

script.Parent.Activated:Connect(function()
	Animation:Play()
	script.Parent.ActivatedTool.Value = true
end)
script.Parent.Unequipped:Connect(function()
	Animation:Stop()
	script.Parent.ActivatedTool.Value = false
end)
script.Parent.Deactivated:Connect(function()
	Animation:Stop()
	script.Parent.ActivatedTool.Value = false
end)

Server Script (Inside Handle of Tool)

local cooldown = true
function onTouch(hit)
	if hit.Name == "tree" then
		if cooldown == true then
			script.Parent.Parent.ActivatedTool.Changed:Connect(function()
				if script.Parent.Parent.ActivatedTool.Value == true then
					cooldown = false
					script.Parent.ChopSFX:Play()
					hit.numberOfHits.Value = hit.numberOfHits.Value - 1
					print("tree has been touched by axe")
					wait(1.3)
					cooldown = true
				end
			end)
		end
	end
end
script.Parent.Touched:Connect(onTouch)

I really need help on this one. Can anyone explain to me why this script isn't working?

Regards, Sensei_Developer

2 answers

Fixed it, Make sure everything is parented in the right place.

local cooldown = true
script.Parent.Touched:Connect(function(P)
	if cooldown == true then
		script.Parent.Parent.ActivatedTool.Changed:Connect(function()
			coroutine.resume(coroutine.create(function()
				while true do
					wait(0.1)
					if script.Parent.Parent.ActivatedTool.Value == true then
						cooldown = false
						P.Parent.numberOfHits.Value = P.Parent.numberOfHits.Value - 1
					    wait(1.3)
					    cooldown = true
					end
				end
			end))
		end)
	end
end)

6 comments

0

Wrong syntax, it should be coroutine.resume(coroutine.create(function() end)) you can’t create a resuming coroutine

0

Oh sorry it's a typo, Thanks for telling me it's now fixed!

0

Okay, I'll try it out.

0

Read the bottom post:

0

Did you fix it?

0

It won't work due to FilteringEnabled. Any changes you make on the client the server cannot see. You can use RemoteEvents to make a workaround though

Responding to Trading_Opportunity;

The code doesn't work! I believe I copied everything down but it still doesn't work. Please help!

Copied script:

local cooldown = true
function onTouch(hit)
	if hit.Name == "tree" then
		if cooldown == true then
			script.Parent.Parent.ActivatedTool.Changed:Connect(function()
				coroutine.resume(coroutine.create(function()
					while true do
						wait(0.1)
						if script.Parent.Parent.ActivatedTool.Value == true then
							cooldown = false
							script.Parent.ChopSFX:Play()
							hit.Parent.numberOfHits.Value = hit.Parent.numberOfHits.Value - 1
							print("tree has been touched with the tool activated")
							wait(1.3)
							cooldown = true
						end
					end
				end))
			end)
		end
	end
end
script.Parent.Touched:Connect(onTouch)

4 comments

0

Hm.

0

I am currently testing in studio, Will fix it and give you the fixed version as fast as possible. Please remain patient.

0

Okay!

0

You done yet?

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