Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Need help making a pokeball spawn where i click?

So what im trying to do is make it so wherever i click with a tool a pokeball from my hand disapears and a new pokeball goes to where i clicked at

What i need help with is getting my click position and making the pokeball spawn at that position cause currently when i click is spawns in a specific location and i cant get it to spawn where i click

here are my scripts

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PokeThrow = ReplicatedStorage:WaitForChild("PokeBallThrow")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local function TheFunction()
	PokeThrow:FireServer(mouse.Hit.Position)
end

mouse.Button1Down:connect(TheFunction)

then it goes to this

local RS = game:GetService("ReplicatedStorage")
local PokeThrow = RS:WaitForChild("PokeBallThrow")

PokeThrow.OnServerEvent:connect(function(player, MousePosition)
	local mouse = player:GetMouse()
	local Character = player.Character
	local Tool = Character:WaitForChild("Pokeball")
	local ToolHandle = Tool:WaitForChild("Handle")
	local PokeballO = RS.Pokeball
	local Pokeball = PokeballO:Clone()
	Pokeball.Parent = game.Workspace.PokeballClones
	Pokeball.Position = Vector3.new(MousePosition)
	Pokeball.Anchored = false
	Pokeball.CanCollide = true
	
	
end)

bily117#5405 is my discord if u need to contact me

1 comment

0

Please use :Connect() insteas of :connect()

1 answer

✓ Accepted answer

Well- first of all, this code is a bit weird because "PokeThrow.OnServerEvent:connect(...)" is using a deprecated area- :connect(...) should be :Connect, and the thing is I'd suggest using mouse.Hit but not in the way you used it-, I used it in my game once and it works quite well.

local RS = game:GetService("ReplicatedStorage")
local PokeThrow = RS:WaitForChild("PokeBallThrow")

PokeThrow.OnServerEvent:connect(function(player)
    local mouse = player:GetMouse()
    local Character = player.Character
    local Tool = Character:WaitForChild("Pokeball")
    local ToolHandle = Tool:WaitForChild("Handle")
    local PokeballO = RS.Pokeball
    local Pokeball = PokeballO:Clone()
    local MousePosition = mouse.Hit.p
    local posY = nil -- change to Y position you think fits
    local redefinedMousePosition = Vector3.new(MousePosition.X, posY, MousePosition.Z)
    Pokeball.Parent = game.Workspace.PokeballClones
    Pokeball.Position = refedinedMousePosition
    Pokeball.Anchored = false
    Pokeball.CanCollide = true


end)

the reason line 13 on the Y position has a variable called posY is because mouse.Hit also gets the Y axis of the mouse, which is not good, because then it would be stuck in the air then in another place in the air when it happens again and is incredibly buggy. Change the variable on line 12 to be whatever Y Position you'd like the pokeball to go to.

for some odd reason the code is wrong positioning for the vector3, click view source and copy the line from there instead.

Also I removed the un-needed MousePosition Parameter

1 comment

0

Im getting a error " ServerScriptService.PokeThrowSettings:29: attempt to index nil with 'Hit' - Server - PokeThrowSettings:29 " :P

This discussion is preserved as it was. New answers, comments, and votes are closed.