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

Getting user input without a tool?

Asked by
novipak 70
9 years ago

I am currently working on a feature where you can click on certain objects and interact with them in different ways (this is why I have many variables set up, and are not being used). I have first started making interaction with an enemy possible (through setting a combat variable to true and making selection box to red, though I hope to expand it later). When testing this script, scrolling over an NPC will not allow you to see a selectionbox, and clicking it will do nothing, it also shows no errors. I would GREATLY appreciate it if you could figure out what is wrong with the following code:

-- Setting all variables
local player = game.Players.LocalPlayer
local character = player.Character
if not character or character.Parent == nil then
    character = player.CharacterAdded:wait()
end
local charpos = character.Torso.Position
local mouse = player:GetMouse()
local selection = Instance.new("SelectionBox", player.PlayerGui)
selection.Color = BrickColor.new("Medium stone grey")
local combat = script.combat
local talking = script.talking
local reading = script.reading
local questing = script.questing

--check target and make variables when mouse moves
mouse.Move:connect(function()
    local target = mouse.Target
    local hum = target.Parent:FindFirstChild("Humanoid")
    local hrp = target.Parent:FindFirstChild("HumanoidRootPart")
    local enemy = target.Parent:FindFirstChild("enemy")
    local friend = target.Parent:FindFirstChild("friendly")
    local quest = target.Parent:FindFirstChild("quest")
    local book = target.Parent:FindFirstChild("book")
    --check if the target is an npc and not a player
    if target.Parent:IsA("Model") and hum ~= nil and hrp ~= nil then
        selection.Adornee = target.Parent
        -- if the target has the enemy value in it
        if target.Parent.enemy == true then
            --and the player clicks on target
            mouse.Button1Down:connect(function()
                --and the player is close enough
                local targetpos = target.Parent.Torso.Position
                if player:DistanceFromCharacter(targetpos) < 40 then
                    --set combat equal to true, and make the selectionbox red
                    combat.Value = true
                    selection.Ardornee = target.Parent
                    selection.Color = BrickColor.new("Bright red")
                end
            end)
        end
    end
end)

Thank you for reading :)

1
Line 28, you misspelled Adornee and spelled "Ardornee". Also, on line 25 you assumed the target's parent is a player and it has a Torso. I would recommend checking that first. Another tip is for line 34 where you check the magnitude from the character's position and the target's position. That is a rather inaccurate method, I highly recommend using DistanceFromCharacter instead. Aethex 256 — 9y
0
Thanks for the corrections, however, it still doesn't seem to work, and no errors are occurring. Also, I put the line of code that declares targetpos in a place where it is guaranteed to have a torso. novipak 70 — 9y
0
Line 37 is also spelled as "Ardornee" when it should be Adornee. Aethex 256 — 9y
0
It might also help to find out what is working and what is not by printing to the output. You can check to make sure the function is being called correctly, and each if statement is correct or not. Aethex 256 — 9y
View all comments (2 more)
0
I might also add that on line 18, you check the mouse's target object; however, it is possible for the target to be nil, so you should check for that in the case the mouse is not hovering over any object. Aethex 256 — 9y
0
You just assume 'target.Parent.enemy' exists even when it may not. Perci1 4988 — 9y

2 answers

Log in to vote
0
Answered by
iaz3 190
9 years ago

I would suggest making your anonymous function a normal one, and connecting it to mouse.Move AND mouse.Idle

That way it will fire when the mouse is not actively being moved, but the screen viewpoint is.(IE Scrolling, turning the camera, walking, anything where you don't/don't have to move the mouse

Ad
Log in to vote
-1
Answered by
SirGory 15
9 years ago

I find that if I try to use game.Players.LocalPlayertoo quickly it will not work.

I do something like this when I try to get the Local Player

repeat wait() until game:GetService("Players")
repeat wait() until game:GetService("Players").LocalPlayer
local plr = game:GetService("Players").LocalPlayer

Answer this question