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

How do i make the ball shoot from the sword when the user clicks with their mouse?

Asked by 4 years ago

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
0
:playerfromcharacter isn't a function. also, onMouseButton1Click isn't a built-in-function either. JesseSong 3916 — 2y
0
Also, Connect should be :Connect not .Connect JesseSong 3916 — 2y

3 answers

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago

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.

0
Re-edited, check my answer! JesseSong 3916 — 2y
Ad
Log in to vote
1
Answered by 4 years ago

really easy

tool = script.Parent
mouse = player:GetMouse

--function for the ball

tool.Activated:Connect(ball(b))
0
You forgot the "()" t the end of "GetMouse" and you called the function while connecting it to a RBXScriptSignal. youtubemasterWOW 2741 — 4y
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
2 years ago
Edited 1 year ago

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!

Fixed Script:


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:

Date: Sun, May 29th, 2022

Answer this question