I’m working on a tool that shoots a neon ball to where the player’s mouse is on the screen, however I couldn’t really get it work. It used to work completely fine when I was writing the script in ONLY a LocalScript, but once I added a Server script to the tool and added client to server communication It’s been bugging out. The only problem I’ve been facing for the past hour is getting the Hit.Position property of the players mouse. I’ve tried like Every possible solution for this but it just doesn’t want to work.
Here’s the server script:
repeat
task.wait()
until workspace["Alive"] ~= nil
local player2 : Player
local char : Model
local events = {}
for _, z in pairs(script.Parent:GetDescendants()) do
if z:IsA("RemoteEvent") or z:IsA("RemoteFunction") then
events[z.Name] = z
end
end
if script.Parent.Parent.Parent:IsA("Player") then
player2 = script.Parent.Parent.Parent
char = player2.Character or player2.CharacterAdded:Wait()
elseif script.Parent.Parent.Parent.Parent:IsA("Model") and script.Parent.Parent.Parent.Parent:FindFirstChildOfClass("Humanoid") then
local m =script.Parent.Parent.Parent.Parent
player2 = game:GetService("Players"):GetPlayerFromCharacter(m)
end
local data = events["Data"]:InvokeClient(player2, {
["Request"] = "PlayerStuff" -- Just returns a table with the Player's "Player" object and the Player's character.
})
local player
char = data.Character
player2 = data.Player
player = data.Player
local limbs = {}
local sounds = {}
for _, z in pairs(script.Parent:GetDescendants()) do
if z:IsA("Sound") then
sounds[z.Name] = z
end
end
for _, z in pairs(char:GetChildren()) do
if z:IsA("BasePart") then
limbs[z.Name] = z
end
end
local states = {
Charged = false,
Active = false
}
local cooldowns = {
Shoot = false
}
local Moveset = {
["Click"] = function(args)
local BallFolder = workspace:FindFirstChild("Balls") or workspace:WaitForChild("Balls", 25)
local PlayerBulletsContainer
if not BallFolder:FindFirstChild(player.Name.."'s Container") then
PlayerBulletsContainer = Instance.new("Folder")
PlayerBulletsContainer.Name = player.Name.."'s Container"
PlayerBulletsContainer.Parent = BallFolder
else
PlayerBulletsContainer = BallFolder:FindFirstChild(player.Name.."'s Container")
end
local ball = Instance.new("Part")
ball.Shape = Enum.PartType.Ball
ball.Size = Vector3.new(1, 1, 1)
ball.BrickColor = states.Charged and BrickColor.new("Bright red") or BrickColor.new("Institutional white")
ball.Material = Enum.Material.Neon
ball.CFrame = limbs["HumanoidRootPart"].CFrame
ball.Velocity = (events["Data"]:InvokeClient({Request = "MousePosition"}) - limbs["HumanoidRootPart"].Position).Unit * 500
-- The line above throws out the "Unable to cast value to Object" error btw
ball.CanCollide = true
ball.Parent = PlayerBulletsContainer
end,
}
And here’s the client script that sends over information to the aforementioned Server script.
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local events = {}
for _, z in pairs(script.Parent:GetDescendants()) do
if z:IsA("RemoteFunction") or z:IsA("RemoteEvent") then
events[z.Name] = z
end
end
events["Data"].OnClientInvoke = function(args)
if args["Request"] == "PlayerStuff" then
return {
Player = player,
Character = char
}
elseif args["Request"] == "Mouse" then
return player:GetMouse().Hit.Position
end
end
local uis = game:GetService("UserInputService")
local mouse
repeat
task.wait()
mouse = player:GetMouse()
until mouse ~= nil
print("Mouse loaded")
uis.InputBegan:Connect(function(i, g)
if g then return end
if i.UserInputType == Enum.UserInputType.MouseButton1 then
events["Input"]:FireServer({
Input = "Click",
args = {
MousePosition = mouse.Hit.Position
}
})
end
Also, just for good measure or something, here’s a picture of the tool itself in the Explore page
Please help me out bru
This is the line that throws out the error btw:
ball.Velocity = (events["Data"]:InvokeClient({Request = "MousePosition"}) - limbs["HumanoidRootPart"].Position).Unit * 500