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

Touched event fires but the if statements dont work/do what their supposed to do?

I know the Touched event works and fires but the If statements dont work?

The If statements unanchor or anchor a part randomly but this doesnt happen.

script.Parent.Touched:Connect(function(touched)
	local baseplate = workspace.Baseplate
	local rain = workspace.rain
	local stormwinds = workspace.stormwinds
	local impact = workspace.impactsource
	local chance = math.random(100)
	if chance and not baseplate and not rain and not stormwinds and not impact >= 0 then
		touched.Anchored = false
	elseif chance and not baseplate and not rain and not stormwinds and not impact <= 0 then
		touched.Anchored = true
	end
end)

No errors in console either (I already know there were gonna be no errors in the console)

15 comments

0

math.random() should have two parameters filled not one

0

That doesn't do anything (it still does nothing)

0

well maybe try separating those two if statements into a bunch of if statements

0

and if that doesn't work then try doing imapctsource.Value instead of impactsource

0

impact*

0

doesn't do anything either

0

impactsource is a brick not a value, all of the variables are bricks

0

what do you mean "if not baseplate"??? if the baseplate is nil, your script is going to error at line 2. if its not, it will never get past your if statement

2

math.random() can take one argument. Stop spreading misinformation, MCAndRobloxUnited.

0

then how would i get past this (baseplate is in workspace)

0

It can also take no arguments and if that happens generates a number in the interval [0, 1)

0

incapaz help pls

0

As @MCAndRobloxUnited pointed out, you need two parameters, not one, to call math.random, for the random chance generator needs to have a range.

0

Also, you just said "if chance". If you want a random chance, like I assume you do, you would need something along the lines of "if chance == 100 and..."

1

No, this script is supposed to unanchor something with 50% chance which is why its "if chance >= 50 then", also math.random can take one, two, and no arguments

1 answer

✓ Accepted answer

Since I finally know what you're talking about, here. Just put the parts that are allowed to be unachored into a table:

local TouchableParts = {workspace.Baseplate,workspace.stormwinds,workspace.impactsource,workspace.rain}

script.Parent.Touched:Connect(function(part)
  for _,v in pairs(TouchableParts) do
     if v ~= part then
      	local chance = math.random(0,100)
        if chance >= 10 then
        	part.Anchored = false
        end
     end
  end
end)

##Accept and upvote if this helps!

7 comments

0

doesn't work still :/ (and the thing is its supposed to unanchor everything except for things in the table)

0

oh

0

i have 2000 reps now nerd

0

race to 3k ur on

0

pls help

0

what'd u do to fix it??

0

idk

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