So I need the ball to shoot out of the sword when a player clicks using their mouse.This is the script i tried. I think it works but i dont know how to activate it when somebody clicks with their mouse can somebody please help.
script.Parent.Connect:OnMouseButton1Click(ball) function ball(b) local Tool = script.Parent local boi = Tool.Parent local girl = game.Players:playerfromcharacter(boi) local missile = Instance.new("Part") local spawnpos = boi.PrimaryPart.Position spawnpos = spawnpos + (b * 10) missile.Position = spawnpos missile.Size = Vector3.new(4,4,4) missile.Velocity = b * 10 missile.BrickColor = BrickColor.new(1004) missile.Shape = 0 missile.BottomSurface = 0 missile.TopSurface = 0 missile.Name = "missile" missile.Reflectance = 0 local force = Instance.new("BodyForce") force.force = Vector3.new(0,98,0) force.Parent = missile local creatortag = Instance.new("ObjectValue") creatortag.Value = girl creatortag.Name = "creator" creatortag.Parent = missile missile.Parent = game.Workspace end
You have to use UserInputService
. Try this:
local UserInputService = game:GetService("UserInputService") local function ball(input, gameProcessedEvent) if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessedEvent then local Tool = script.Parent local boi = Tool.Parent local girl = game.Players:playerfromcharacter(boi) local missile = Instance.new("Part") local spawnpos = boi.PrimaryPart.Position spawnpos = spawnpos + (b * 10) missile.Position = spawnpos missile.Size = Vector3.new(4,4,4) missile.Velocity = b * 10 missile.BrickColor = BrickColor.new(1004) missile.Shape = 0 missile.BottomSurface = 0 missile.TopSurface = 0 missile.Name = "missile" missile.Reflectance = 0 local force = Instance.new("BodyForce") force.force = Vector3.new(0,98,0) force.Parent = missile local creatortag = Instance.new("ObjectValue") creatortag.Value = girl creatortag.Name = "creator" creatortag.Parent = missile missile.Parent = game.Workspace end end UserInputService.InputBegan:Connect(ball)
The if statement checks if the input is a MouseClick and if the player isn't in chat. Make sure it's a LocalScript
. If you want to make the ball appear for the server, use a RemoteEvent
.
really easy
tool = script.Parent mouse = player:GetMouse --function for the ball tool.Activated:Connect(ball(b))
Answer:
Step-by-step explanation:
First of all, OnMouseButton1Click is not a valid built-in function from ROBLOX. This will return nil.
Secondly, Connect should be :Connect
NOT .Connect
Thirdly, playerfromcharacter is not a function. I believe you meant :GetPlayersFromCharacter.
NOTE: When connecting to a custom function, you need to connect it to that function or else it wouldn't actually fire. (Put Line 1 in the last line of your script, with the improvements I mentioned above).
(Keep in mind, that MouseButton1Click can only fire once the event is called from a GUIObject, so things like Frames, ImageLabels wouldn't work!)
Unlike @youtubemasterwow's answer, it doesn't matter if you use UserinputService for this case since it will still run!
local function ball(b) local Tool = script.Parent local boi = Tool.Parent local girl = game.Players:GetPlayerFromCharacter(boi) local missile = Instance.new("Part") local spawnpos = boi.PrimaryPart.Position spawnpos = spawnpos + (b * 10) missile.Position = spawnpos missile.Size = Vector3.new(4,4,4) missile.Velocity = b * 10 missile.BrickColor = BrickColor.new(1004) missile.Shape = 0 missile.BottomSurface = 0 missile.TopSurface = 0 missile.Name = "missile" missile.Reflectance = 0 local force = Instance.new("BodyForce") force.force = Vector3.new(0,98,0) force.Parent = missile local creatortag = Instance.new("ObjectValue") creatortag.Value = girl creatortag.Name = "creator" creatortag.Parent = missile missile.Parent = game.Workspace print("yes") end script.Parent.MouseButton1Click:Connect(ball)
Grammatical errors fixed: