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

How do I fix this mouse.Target script?

Asked by 9 years ago

What this is supposed to do is, whenever someone puts their mouse over a part which name is "great" it sets a value to true then back to false after 10 seconds

Error:

21:14:06.824 - Players.Player1.PlayerGui.LocalScript:4: attempt to index field 'Target' (a nil value)
21:14:06.825 - Stack Begin
21:14:06.826 - Script 'Players.Player1.PlayerGui.LocalScript', Line 4
21:14:06.827 - Stack End

This is in a LocalScript inside of StarterGui

local mouse = game.Players.LocalPlayer:GetMouse()
gate = game.Lighting.gate

if mouse.Target.Name == "okay" then
    gate.Value = true
    print("good")
    wait(10)
    gate.Value = false
    print("Better")
    repeat wait() until game.Lighting.gate.Value == true
end

Also, I am not sure if this script can be used unlimited times, I am pretty positive line 10 covers that, not sure

0
if mouse.Target == nil then return end iuclds 720 — 4y

1 answer

Log in to vote
1
Answered by
RoboFrog 400 Moderation Voter
9 years ago

Well, first off, you'll want to add this to your script --

if mouse.Target == nil then end
    if mouse.Target ~= nil then
    -- Code here
end

The problem with mouse.Target is that whenever it touches a nil value (for example, the skybox) it'll return a nil value.

Your line --

repeat wait() until game.Lighting.gate.Value == true

Means that none of the code under that will run until that value == true. In this case, you're just keeping the function from ending properly. It'll automatically loop, but as of now, at very high speeds.

You'll want to add a debounce to the script so that it can't activate too many times at once.

Also, the line repeat wait() until game.Lighting.gate.Value == true will want to be used inside of the gate itself. That means that the gate will not open until that value is true (assuming you have the open code below that line).

However, assuming you use the Changed event, you won't need that line. That line is mostly used for code that is already running, but you want to have a pause in order to wait for other things to happen before going forward. An example of the changed event in use is as follows --

game.Lighting.gamevalue.Value.Changed:connect(function(value) -- this function will return the value that it was changed to. It's not always needed, but can be useful at times.
end)

In case it isn't known, you'll want to use 2 scripts and a variable for this -- the local script that handles the part finding, the variable that detects changing, and the script in the gate that sees whenever the variable changes and initiates code accordingly.

That should be all, but if you continue to get errors, let me know and I can help out.

EDIT: This might also be in part to the fact that it's in a StarterGui instead of a tool. I'm not sure if mouse.Target will be able to get the local mouse if not inside of a tool. However, this is just a guess, and should only be considered if all else fails.

Ad

Answer this question