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

My injector tool for an npc doesn't work, how can i fix it?

Asked by 5 years ago
Edited 5 years ago

Now, this is probably another dumb question i made but how can i make it work correctly? I made it print("hi") incase before i posted this, but failed to print..

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()


mouse.Button1Down:Connect(function()
if mouse.Target.Parent == "Test" then --Probably can be a table ~intepcs
    local Test = workspace.Folder.Test
    Test.Humanoid.Health = 25
end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

The reason for this is line 6.

if mouse.Target.Parent == "Test" then

The Mouse.Target property holds a reference, a reference to the BasePart the mouse is hovering over. In order to fix this, you should check the Name of the Target.

local UserInputService = game:GetService("UserInputService")
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()

UserInputService.InputBegan:Connect(function(input, gpe)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        -- If they clicked the button...

        if not Mouse.Target then return end 
        -- If they are pointing to the sky, Target is nil.

        if Mouse.Target.Parent.Name == "Test" then -- Checks the name.
            Mouse.Target.Parent.Humanoid.Health = 25 
        end
    end
end)

And finally, remember to indent your code properly. Your end on line 9 is not inside of the event, it is shifted to the left. Even if it's one line, it is important to have indented code, for the sake of readability.

Hope this helped!

Ad

Answer this question