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

Why is this "touched" script not working?[SOLVED]

fval = game.Players.LocalPlayer.PlayerGui.Values.Flag.Value

function touch()
	fval = 1
	print("hi")
end

script.Parent.BFlagCanister:connect(touch)

In a local script and there is no output message.

Values is the thing holding all the other values. Flag is an intvalue. BFlagCanister is the block. It is anchored, has a mesh, is no collided .

1 comment

0

Mind explaining BFlagCanister please.

4 answers

✓ Accepted answer

TheContinentofEurope is getting there, but there is a problem that I can spot in the script.

Shortening your script:

Before I get into what the error is, we're going to shorten the script. Shortening your scripts improves efficiency and is helpful when you have tons of scripts running at once.

You can use a function and then use an event to connect it, it works none the less. But, you can do this all in one go using an anonymous function.

script.Parent.Touched:connect(function(hit) --This is an anonymous function. We're going to write a function in the brackets and close it off at the end.
	local player=game.Players:GetPlayerFromCharacter(hit.Parent)
	local fval=player.PlayerGui.Values.Flag.Value
	fval=1
	print("hi")
end) --Note that the parenthesis/bracket is closed at the end of the function so that it's incorporated into the event's connection properly.

The error itself:

The actual error in your script is that you're trying to do a "shortcut". If you set a variable as a value of a Value object, you'd think it would update if you did "fval=1". This is not the case, if you do this, the value won't update because the computer will handle that line as if you're setting the variable. If you printed fval after you did "fval=1" with your current script, it would print 1, but if you go to your value, the value won't have changed.

To fix this, we will do the following.

script.Parent.Touched:connect(function(hit)
	local player=game.Players:GetPlayerFromCharacter(hit.Parent)
	local fval=player.PlayerGui.Values.Flag
	fval.Value=1 
	print("hi")
end)

See how we're setting the value property on the next line instead of setting it as the variable? If we do it this way, it should work. Let me know if there's any problems and make sure to check your output window for errors in your script.

Good luck!

Learn more on anonymous functions here.

2 comments

0

Thank you for correcting me!

1

No problem.

You're missing a Touched at the end of your script, I believe that is your Problem,

function touch(hit)--Catches whoever touches it
	local player=game.Players:GetPlayerFromCharacter(hit.Parent)
	--the GetPlayerFromCharacter is pretty useful in cases, like this
	local fval=player.PlayerGui.Values.Flag.Value
	fval=1
	print("hi")
end
script.Parent.Touched:connect(touch)

You can learn more about functions/events at ROBLOX Wiki

15 comments

0

still doesn't work

0

try Removing the BFlagCanister

0

updated

0

Wait I see your error.

0

Touched, only works with a Part.

0

The issue could also be that he is attempting to be calling 'LocalPlayer' from a Server-Sided script, or, as you've stated, he is attempting to call event line 'Touched' on a non-BasePart type instance.

0

still doesn't work when i did it on the individual part

0

Can you please explain a bit more , I don't get what your trying to do.

0

I cahnged the model into a part. Im trying to get it so that when i touch the part it will print the message and give the value of 1 to the flag value

0

Ok, Then use a script, i'll update my answer, into a script version.

0

thx

0

work?

0

nah but thanks for trying

0

I think I see the error, I'll post an answer now.

0

ok!

You messed up on your connecting part.

fval = game.Players.LocalPlayer.PlayerGui.Values.Flag.Value

function touch()
    fval = 1
    print("hi")
end

script.Parent.Touched:connect(touch)

I believe your problem is at line 4. You are not allowed to hold a property in a variable.

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