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

how do i make this script work on Click instead of moving the mouse?

Asked by 7 years ago
Edited 7 years ago

by selecting a tool, this script moves the warrior (line 2) even though it makes him go to the edge of the map and fall off (that is a problem also)

how could i make this work by just clicking the mouse on the baseplate with no tool selected?

tool = script.Parent
warrior = game.Workspace.Warrior

playerMouse = game.Players.LocalPlayer:GetMouse()

tool.Equipped:connect(function(m)
    m.Icon = "http://www.roblox.com/asset/?id=79658449"

    m.Move:connect(function()
        print("X Mouse Position: "..playerMouse.X.."",", ","Y Mouse Position: "..playerMouse.Y.."")
        warrior.Humanoid:MoveTo(Vector3.new(playerMouse.X,playerMouse.Y))

    end)
end)

i tried playerMouse:connect(click) it didnt even budge the warrior except with cframe and preset numbers

thanks to all that help me :)

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Your function, "playerMouse:connect(click)" is wrong. Here, I will give you a short mouse tutorial.


First of all, make sure this is a LocalScript. Also, you should define the player. It's not needed, but you never know;

local player=game.Players.LocalPlayer --Get the player (local script only!)

Getting the Mouse is fairly similar;

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

Now, the mouse has many properties and events. The event you need is Button1Down;

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

mouse.Button1Down:connect(function() --When the player clicks..
    print("Left Mouse Button Clicked!")
end)

The mouse also has a Hit property that gets the Mouse's position;

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

mouse.Button1Down:connect(function() --When the player clicks..
    print("Left Mouse Button Clicked!")
    print(mouse.Hit) --Prints the CFrame of the Mouse
end)

Putting it all together


local player=game.Players.LocalPlayer --Get the player
local mouse=player:GetMouse() --Get the mouse
local warrior=workspace.Worrior--Get the warrior

mouse.Button1Down:connect(function() --When the player clicks..
    warrior.Humanoid:MoveTo((mouse.Hit).p)
end)
0
this worked. thanks :) just need to make it animated instead of just dragging his feet, but i think i can figure that out User#12356 0 — 7y
0
one problem though. it only moves one warrior ( i had 3 in the workspace) it only moved one. i tried :getchildren if name = warrior and stuff. it either broke the script or still moved one. User#12356 0 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Functions must be connected to an event.

Use the Button1Down event of the mouse and connect it to a function.

tool = script.Parent
warrior = game.Workspace.Warrior

playerMouse = game.Players.LocalPlayer:GetMouse()

tool.Equipped:connect(function(m)
    m.Icon = "http://www.roblox.com/asset/?id=79658449"

    m.Button1Down:connect(function()
        print("X Mouse Position: "..playerMouse.X.."",", ","Y Mouse Position: "..playerMouse.Y.."")
        warrior.Humanoid:MoveTo(Vector3.new(playerMouse.X,playerMouse.Y))

    end)
end)
Basically just swapped the move event with the Button1Down event.

Good Luck!

If I helped, please don't forget to accept my answer.
0
its not working. its not even printing the positions at line 10 User#12356 0 — 7y

Answer this question