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.

01script.Parent.Connect:OnMouseButton1Click(ball)
02function ball(b)
03    local Tool = script.Parent
04    local boi = Tool.Parent
05    local girl = game.Players:playerfromcharacter(boi)
06 
07    local missile = Instance.new("Part")
08 
09 
10    local spawnpos = boi.PrimaryPart.Position
11    spawnpos = spawnpos + (b * 10)
12    missile.Position = spawnpos
13    missile.Size = Vector3.new(4,4,4)
14    missile.Velocity = b * 10
15    missile.BrickColor = BrickColor.new(1004)
View all 33 lines...
0
:playerfromcharacter isn't a function. also, onMouseButton1Click isn't a built-in-function either. JesseSong 3916 — 3y
0
Also, Connect should be :Connect not .Connect JesseSong 3916 — 3y

3 answers

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

You have to use UserInputService. Try this:

01local UserInputService = game:GetService("UserInputService")
02 
03local function ball(input, gameProcessedEvent)
04    if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessedEvent then
05        local Tool = script.Parent
06        local boi = Tool.Parent
07        local girl = game.Players:playerfromcharacter(boi)
08 
09        local missile = Instance.new("Part")
10 
11 
12        local spawnpos = boi.PrimaryPart.Position
13        spawnpos = spawnpos + (b * 10)
14        missile.Position = spawnpos
15        missile.Size = Vector3.new(4,4,4)
View all 38 lines...

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 — 3y
Ad
Log in to vote
1
Answered by 4 years ago

really easy

1tool = script.Parent
2mouse = player:GetMouse
3 
4--function for the ball
5 
6tool.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
3 years ago
Edited 2 years 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:

01local function ball(b)
02    local Tool = script.Parent
03    local boi = Tool.Parent
04    local girl = game.Players:GetPlayerFromCharacter(boi)
05 
06    local missile = Instance.new("Part")
07 
08 
09    local spawnpos = boi.PrimaryPart.Position
10    spawnpos = spawnpos + (b * 10)
11    missile.Position = spawnpos
12    missile.Size = Vector3.new(4,4,4)
13    missile.Velocity = b * 10
14    missile.BrickColor = BrickColor.new(1004)
15    missile.Shape = 0
View all 34 lines...

Grammatical errors fixed:

Date: Sun, May 29th, 2022

Answer this question