Ad
Log in to vote
0

Updating a variable when the Mouse.Target changed? [SOLVED]

Asked by 6 months ago
Edited 6 months ago

A have a placement script and it lets you place on objects that the mouse.Target.Name == "Baseplate" so what I am trying to do since i only call the function once, it update the mouse target over and over so that the script knows what you can place on.

Heres the script and again I am trying to update the Mouse.Target

wait(5)
print("PlacementStarted")
local PlacementHanlder = require(script:WaitForChild("PlacementModule"))
local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local CraftingGui = Player.PlayerGui.ScreenGui:WaitForChild("PlayerCrafting")

local Items = game:GetService("ReplicatedStorage").Items
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Model
local Signal
local CurrentItemName

local function UpdateTarget()
    Base = Mouse.Target
end

Mouse.Move:Connect(UpdateTarget)    
Mouse.Idle:Connect(UpdateTarget)    

function BeginPlacement()
    local ItemName = CraftingGui.Info.ItemName.Text

    if script.Parent.EnablePlacement.Value == true then
        RunService:BindToRenderStep("Placement", Enum.RenderPriority.Camera.Value, function()
            if Mouse.Target and Mouse.Target.Name == "Baseplate" then

                Plane = PlacementHanlder.new(Base, workspace.Base.ItemHolder, 1)

                CancelPlacement()

                Model = Items[ItemName]:Clone()

                CurrentItemName = ItemName  

                for _, object in pairs(Model:GetChildren()) do 
                    if object:IsA("BasePart") then
                        object.CanCollide = false
                    end
                end

                Model.Parent = workspace.CurrentlyPlacing

                local Signal = Plane:enable(Model)

                Signal:Connect(function(Location, _)
                    game:GetService("ReplicatedStorage").ClientPlaced:FireServer(CurrentItemName, Location[1])      

                    CancelPlacement()
                end)
            end
        end)
    end
end

function CancelPlacement()
    script.Parent.EnablePlacement.Value = false
    if (CurrentItemName) then
        Plane:disable()

    if (Model.Parent) then
        Model:Destroy()
    end

        RunService:UnbindFromRenderStep("Placement")
        Model = nil
        Signal = nil
        CurrentItemName = nil       
    end
end

script.Parent.EnablePlacement.Changed:Connect(BeginPlacement)

Most of the important info is at the top.

Let me add that sometimes it works but most of the time it doesnt so I am confused.

Thank you for your time :)

Answer this question