Im Making Some SpellBook, When The Player Is Holding Spell Book, Lighting Will Appear, Also, Question 2, How do you set the CLONED Model Position to the Mouse?
Advanced Thanks!
Im Making Some SpellBook, When The Player Is Holding Spell Book, Lighting Will Appear, Also, Question 2, How do you set the CLONED Model Position to the Mouse?
Advanced Thanks!
You would need to use Equipped event which will fire when you equip the tool, and the UserInputService to check if the player used the right mouse button.
If you don't know much about the UserInputService here you go
local tool = script.Parent --or wherever it is
local UserInputService = game:GetService("UserInputService")
tool.Equipped:Connect(function()
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton2 then --MouseButton2 enum is the right mouse button
--code here
end
end)
end)
So, for the answer to your first question, I know if you use Tool.Activated, you cannot get Mouse.Hit.p, so I would use the Mouse object for simplicity, considering I cannot find an effective way to use UserInputService to get 3D mouse positions. You can use Mouse.Button1Down to get input, and then use Mouse.Hit.p, create a Part with a SpotLight as a child, and set the part's position to where the player clicked, like so (in a LocalScript):
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
local position = mouse.Hit.p --A Vector3 value of the Mouse position in 3D space
local part = Instance.new("Part", workspace)
part.Position = mouse.Hit.p
local light = Instance.new("SpotLight", part)
end
At this point, whenever a player clicks, a part with a light appears wherever the player clicks. But you wanted for the player to holding the spellbook to be able to create a light, so we'll need to add a few things.
For this example, I'm going to call the spellbook (a tool), Spellbook. We need to check for the spellbook being equipped (by checking the Player's Character for the tool, because when you equip a Tool, it is moved from the Player's Backpack to the character Model. Here's how you would fix that:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
mouse.Button1Down:Connect(function()
if pcall(character.Spellbook) ~= nil then
local position = mouse.Hit.p --A Vector3 value of the Mouse position in 3D space
local part = Instance.new("Part", workspace)
part.Position = mouse.Hit.p
local light = Instance.new("SpotLight", part)
end
end
If you didn't notice, I used a pcall() on line 6 of the code to make sure that if the player didn't have the Spellbook, which would throw an error, doesn't throw an error. pcall() prevents errors from being thrown.
Hope I helped you on your first question! But there is still the second question I need to answer.
You can move an entire model, without running a loop to move every part by setting one of the parts (usually the center) to the Model's PrimaryPart. Then you can set the Model's PrimaryPart position (and direction, etc.) with the Model object's function: Model:SetPrimaryPartCFrame() like so:
local Model = workspace.Model --Considering you have some Model, you can change this as well, and the Model needs a PrimaryPart
wait(5) --After five seconds...
Model:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,20,0)) --It moves the model up into the air!
So I hope I have helped you with your problems!
cooly456
P.S. I think you should have separate posts for separate questions!
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
The question asked for the LEFT mouse button...
dude this was a pure script request which isnt allowed