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

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

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

✓ Accepted answer

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!

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