#Having issues getting mouse position to send to server side function

1 messages · Page 1 of 1 (latest)

south arch
#

I am currently making a script for my game that spawns a bullet on mouse click and I want it to shoot in the direction that the mouse is. The issue im having however is with passing the mouse pos through FireServer to the server so i can spawn the ball and apply the impulse on the server for the other player to see. Can anyone help me? Here are my scripts:

Local Script

-- Rest of code is irrelevant, but can give if asked

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
        local mouse = LocalPlayer:GetMouse()
        local targetPosition = mouse.Hit.Position
        print(targetPosition)
        ballHandler:FireServer(LocalPlayer, targetPosition)
    end
end)

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("BallHandler")

local function spawnAndShootBall(LocalPlayer, targetPosition)
    
    local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
    local ball = game.Workspace.Bullets:WaitForChild("Balling"):Clone()
    ball.Parent = game.Workspace.Bullets
    ball.Anchored = false
    ball.BallScript.Enabled = true
    ball.Position = character:WaitForChild("RightHand").Position + Vector3.new(0, 1, 0)
    
    if ball then
        print("Ball spawned")
        local direction = (Vector3.new(0, targetPosition.Y, targetPosition.Z) - Vector3.new(0, ball.Position.Y, ball.Position.Z)).unit * 50  
        ball:ApplyImpulse(direction)
    else
        warn("Ball not found")
    end
end

remoteEvent.OnServerEvent:Connect(function(player, targetPosition)
    spawnAndShootBall(player, targetPosition)
end)

The main issue that happens is when i print targetPosition in server script, it just print the player, and not the value i tried to send over.

carmine gust
#

fireserver's first parameter on onserverevent is always player

#

you're sending player twice

#

just do fireserver:(mousetarget)

south arch
#

holy it worked

#

thank you so much

#

I now have the value successfully passed over to my server script

#

let me just make sure I can succesfully apply the impulse part and then ill mark it as solved

#

Is there any specific reason as to why there keeps on being a delay before the impulse actually get applied

south arch
#

Can you explain the reason

rare fern
#

what impulse

south arch
#

At the end of my server script i apply an impulse to the bullet

marsh karma
#

uwu

south arch
#

💀

marsh karma
#

I dont see any delay

#

wym

rare fern
#

Its kinda of a bug i think

south arch
rare fern
#

u can fix it but then exploiters can abuse it

south arch
#

theres a slight delay at times and then an impulse is applied

marsh karma
#

'

south arch
south arch
rare fern
#

but if its a big delay then

#

its probably something else

south arch
cinder zephyrBOT
#

studio** You are now Level 1! **studio

south arch
#

So youd recommend just deleting that?

marsh karma
#

no

#

just do like

#

if not LocalPlayer.Character then return end

south arch
#

okay

marsh karma
#

and you are looking for wait for child right hand but right hand might not exist in a R6 avatar

south arch
#

Im probably going to be adding some sort of gun/prop where the bullet is shot from once I get to that

#

so thats not the biggest issue rn

#

This is just temporary while i figure out the main parts

marsh karma
#

lar

#

alr

south arch
#

Anyway is there any alternative way I could do the application of the impulse to avoid delay or do I just have to live with it?

marsh karma
#

and you dont need to fire the player

south arch
#

?

cinder zephyrBOT
#

studio** You are now Level 2! **studio

marsh karma
# south arch ?

you can just do


Mouse.Button1Down:Connect(function()
    ballHandler:FireServer(Mouse.Hit.Position)
end)
#

and whenever you fire to server the first variable the server gets is always the player

#

you dont need to fire the player to the server

south arch
#

Yea I already made it so I dont fire the player, that was the fix to my original issue

marsh karma
#

and also

#

you dont need to do this

remoteEvent.OnServerEvent:Connect(function(player, targetPosition)
    spawnAndShootBall(player, targetPosition)
end)
#

you can just do

south arch
#

directly i assume

marsh karma
#
remoteEvent.OnServerEvent:Connect(spawnAndShootBall)
south arch
#

Yea

marsh karma
#

there are a bunch of improvments you can make

#

ig I have mentioned them all

south arch
#

Yea im still pretty new to roblox studio, started like a couple weeks ago. Happy to always learn new things!

#

The applied impulse is very inconsistent by the way

marsh karma
#

oh and btw you dont need to do game.Workspace

#

you can just write workspace

marsh karma
south arch
#

For context, the game is a 2D game, so the camera is locked. Whenever i shoot sometimes it just shoots the complete opposite direction than where im clicking

south arch
south arch
#

and then just doesnt work correctly

marsh karma
#

are you trying to make bullets?

south arch
#

Yea theyre supposed to be bullets

#

Do you know the game on steam called rounds?

marsh karma
#

no

south arch
#

Okay

#

U can look it up iyw, but we are essentially remaking that