Hey Devs Im trying to make it so that when you click the Button on the screen it sets the “TowerToSpawn” part’s CFRAME to the CFRAME of the mouse instead it set’s it to the CFRAME of the player camera it worked before but at this point I tried debugging for ours but idk can you help me find the bug?
--[[
NOTE!
Remember to put this sript in the TowerGui after syncing Rojo to Roblox Studios
]]
--[SERVICES AND VARIABLES]-----------------------------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local camera = workspace.CurrentCamera --3D cam in workspace
local Towers = ReplicatedStorage:WaitForChild("Towers")
local gui = script.Parent
local plr = game.Players.LocalPlayer
local CharModel = plr.Character
---Highlighter for the TowerToSpawn
local TowerHighlight = Instance.new("Highlight")
TowerHighlight.Parent = nil
local TowerToSpawn = nil --No Tower has been clicked by the player yet
local plrmouse = plr:GetMouse()
local MousePos = UserInputService:GetMouseLocation()
local Placers = workspace.YellowMap.Placers:GetChildren()
--------------------------------------------------------------------------------------
local function ClientMouseRaycast(instances)
---Raycast paramters so it ignores some stuff in the game
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
----Table of Instances to ignore in the game
local Instances = {
--[[
workspace.Part1,
workspace.Part2
]]
TowerToSpawn,
CharModel
}
raycastParams.FilterDescendantsInstances = Instances
local mouseRay = camera:ViewportPointToRay(MousePos.X, MousePos.Y)
local RaycastRes = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
return RaycastRes
end
---This handles what what Tower is spawned and what happens to it
local function AddPlaceTower(Name)
local TowerExist = Towers:FindFirstChild("BlackTower") --Gets the Tower in RS
if TowerExist then
TowerExist = TowerExist:Clone()
TowerExist.Parent = Workspace.YellowMap.Towers
TowerExist.CanCollide = false
TowerToSpawn = TowerExist ---Sets the clown tower to the Global TowerSpawnVariable
end
end
---------[WHEN TOWER SPAWN BUTTON IS CLICKED]----------
gui.BlackTowerButton.Activated:Connect(function()
AddPlaceTower("BlackTower")
print("BUTTON CLICKED")
end)
------[RENDER STEP THAT RUNS THE RAYCAST]----------
RunService.RenderStepped:Connect(function()
local RayResult = ClientMouseRaycast({TowerToSpawn}) --Can add stuff to ignore by doing a comma and what you want the raycast to ignore --Stores what the raycast is getting under the variable
--------This loops through each place folder inside the Map Folders
for _, placers in Placers do
local placer = placers ---This is a variable containing all the placers found under the folder
if RayResult then ---If the Ray has found a result
-----Check if the TowerToSpawn has a tower if so runs the following
if TowerToSpawn then ---If there's a TowerToSpawn and it's not equals to nil then it sets the Tower to the mouse CFrame
local x = RayResult.Position.X ---X position of what the Ray is getting
local y = RayResult.Position.Y ---Y position of what the Ray is getting
local z = RayResult.Position.Z ---Z position of what the Ray is getting
---In this part of code the CFame of the Raycast will be equal to the CFrame of the cloned Tower and sets it to the CFrame of the mouseand it will follow the mouse now bcs of the Rneder step
local MouseCFrame = CFrame.new(x,y,z) --Cframe of the mouse in the game
TowerToSpawn.CFrame = MouseCFrame
TowerHighlight.Parent = TowerToSpawn
local PlacableVal = RayResult.Instance:GetAttribute("UnitsPlaced")
print("Units are placed: ", PlacableVal)
end
end
----This is the mouse Vector3 Raycast on the screen
warn(ClientMouseRaycast())
end
-------This event runs when the player clicks his left button of the mouse and sets the tower3
end)