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

Moving KillBrick Not Resetting back to normal?

[EDIT] I AM NOT USING 2 SCRIPTS. THESE ARE THE SAME SCRIPT JUST DIFFERENT SECTIONS

So i have a path (10 blocks wide). I have 1 block changed colour at a time and making it's way to other side, and it cycles through forever. Changing the colour works fine.

My problem is that I only want the block that has changed colours to be a KillBrick; however, the blocks never reset back to a normal one.

Here's my killbrick part of the code

local function kill(hit)
	local t = currentBrick.BrickColor == BrickColor.new("Really red")
	if killOn and t then
		hit.Parent.Humanoid.Health = 0
	end
	print("hit", killOn, t, currentBrick.BrickColor)	
end

Print statement is for testing purposes.

Here is how i'm alternating blocks

while true do
	
	for index, brick in pairs(objects) do
		--THIS SHOULD MAKE THE KILL BRICK
		killOn = true
		brick.BrickColor = BrickColor.new("Really red")
		currentBrick = brick
		brick.Touched:Connect(kill)
		wait(2)

		--THIS SHOULD RETURN THE BRICK BACK TOA NORMAL ONE
		killOn = false
		brick.BrickColor = BrickColor.new("White")
		currentBrick = brick
		brick.Touched:Connect(kill)
	end
end

What exactly am i doing wrong here?

2 answers

Why are you using 2 seperate scripts? Wouldnt it be easier to just make 1 script and work with variables there?.

I see you're trying to use the variable KillOn to know if the part should kill or shouldnt.

Simply delete your "killscript" and make a onTouch function in the other script. Everytime you change the bricks stance ( can kill or cant ) you simply change an variable Value. Example: KillOn true or false, just like you already done it.

lets get back to the ontouch function.

You'll want to make a ontouch function and check if it a player who touched the part AND if killOn == true or false. From there u can ether kill or do nothing.

Im on my phone so unable to write code but I think thats far enough to make it work for you :))

[Edit: this is my original answer; it's incorrect] killOn is not shared between scripts. You need to use ModuleScripts, a BindableEvent, or a BoolValue to communicate between the scripts.

Alternatively, have the kill script check its colour to determine whether it should be deadly or not.

[End of original answer]


The problem is that currentBrick is a global variable, so you're overwriting its value every time you assign to it. You're wanting to capture its current value in a closure; to do that you'll need to make it local and have the function defined in its scope:

-- in while loop
local currentBrick = brick
brick.Touched:Connect(function(hit)
	-- kill functionality here; can use 'currentBrick' and will get the one you wanted
end)

2 comments

0

I am not using 2 scripts. I was merely showing to parts of the same script instead of posting the entire thing.

0

Sorry about that; edited/updated answer

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