Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago
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 .

0
Mind explaining BFlagCanister please. woodengop 1134 — 9y

4 answers

Log in to vote
2
Answered by 9 years ago

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.

0
Thank you for correcting me! woodengop 1134 — 9y
1
No problem. Spongocardo 1991 — 9y
Ad
Log in to vote
2
Answered by
woodengop 1134 Moderation Voter
9 years ago

You're missing a Touchedat 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

0
still doesn't work rabidhalofan12345 55 — 9y
0
try Removing the BFlagCanister woodengop 1134 — 9y
0
updated rabidhalofan12345 55 — 9y
0
Wait I see your error. woodengop 1134 — 9y
View all comments (11 more)
0
Touched, only works with a Part. woodengop 1134 — 9y
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. TheeDeathCaster 2368 — 9y
0
still doesn't work when i did it on the individual part rabidhalofan12345 55 — 9y
0
Can you please explain a bit more , I don't get what your trying to do. woodengop 1134 — 9y
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 rabidhalofan12345 55 — 9y
0
Ok, Then use a script, i'll update my answer, into a script version. woodengop 1134 — 9y
0
thx rabidhalofan12345 55 — 9y
0
work? woodengop 1134 — 9y
0
nah but thanks for trying rabidhalofan12345 55 — 9y
0
I think I see the error, I'll post an answer now. Spongocardo 1991 — 9y
0
ok! woodengop 1134 — 9y
Log in to vote
0
Answered by 9 years ago

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)
Log in to vote
0
Answered by 9 years ago

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

Answer this question