Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Client side of a placement tool works, but the server side doesn't?

Asked by 4 years ago

So I was creating a printer system where a player is given a tool that allows them to place a printer into the workspace. It worked fine when it was in the StarterPack, but when I manually gave them the tool (via cloning and placing in backpack), the server side of this script broke.

Here's the client side of the script:

local tool = script.Parent
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name)
local mouse = player:GetMouse()
local rs = game:GetService("RunService")
local canPreview = true
local canPlace = true
local canUse = true

local preview = game.ReplicatedStorage.Printer:Clone()
tool.Equipped:Connect(function()
    if canUse == true then
        canPreview = true
        preview.Parent = workspace
        preview.Transparency = 0.5
        preview.CanCollide = false
        tool.Activated:Connect(function()
            if canPlace == true then
                canPlace = false
                canUse = false
                game.StarterPlayer.Place:Play()
                tool.Place:FireServer(preview.Position)
                preview.Parent = nil
            end
        end)
    end
end)
tool.Unequipped:Connect(function()
    canPreview = false
    preview.Parent = nil
end)

tool.Place.OnClientEvent:Connect(function()
    tool:remove()
end)

rs.RenderStepped:Connect(function()
    if canPreview == true then
        preview.Position = mouse.Hit.p + Vector3.new(0,preview.Size.Y/2,0)
    end
end)

Here is the server side of the script:

local printInterval = 250
local timeDelay = 5
local maxPrint = 3000
script.Parent.Place.OnServerEvent:Connect(function(player,pos)
    print("uh ok")
    local clone = game.ReplicatedStorage.Printer:Clone()
    clone.Parent = workspace
    clone.Position = pos
    clone.SurfaceGui.Title.Text = player.Name.."'s printer"
    clone.SurfaceGui.Printed.Text = "Money printed: $0"
    clone.MoneyStored.Changed:Connect(function()
        clone.SurfaceGui.Printed.Text = "Money printed: $"..clone.MoneyStored.Value
    end)

    clone.ClickDetector.MouseClick:Connect(function()
        print("Give $"..clone.MoneyStored.Value.." to "..player.Name)
        if clone.MoneyStored.Value >= maxPrint then
            clone:remove()
        end
    end)

    repeat
        clone.MoneyStored.Value = clone.MoneyStored.Value + printInterval
        clone.Printer:Play()
        wait(timeDelay)
    until
    clone.MoneyStored.Value >= maxPrint
    script.Parent.Place:FireClient(player)
end)

My hypothesis is that the script breaks when it becomes a child of a client sided parent (like the backpack), but even if that's true, I don't know how to prevent the script breaking on client side.

Help please!

Answer this question