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

Clone() and Move() Error? Everything is defined and its in a script so its server-sided

Asked by 5 years ago

button = script.Parent local item = game:GetService("ReplicatedStorage"):WaitForChild("Sword") local plr = script.Parent.Parent.Parent.Parent.Parent local frame = script.Parent.Parent

script.Parent.MouseButton1Click:Connect(function() script.Parent.Parent.Parent.ItemEquipper.Text = "EquippedTool = Sword" local clone = item:Clone() clone.Parent = plr.Backpack frame.Visible = false plr.Character:MoveTo(game.Workspace.Tp.Position) end)

This works in studio but not in-game. I have defined everything and its in a script so its server sided but Clone() won't work even tho its on a script. To be honest, nothing works after the function.

1
Please post the code in a code block. User#19524 175 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
button = script.Parent
local item = game:GetService("ReplicatedStorage"):WaitForChild("Sword")
local plr = script.Parent.Parent.Parent.Parent.Parent
local frame = script.Parent.Parent

script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Parent.Parent.ItemEquipper.Text = "EquippedTool = Sword"
        local clone = item:Clone()
        clone.Parent = plr.Backpack
        frame.Visible =  false
        plr.Character:MoveTo(game.Workspace.Tp.Position)
        end)
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Do not use a server script to handle GUI code. Instead, use a local script. Insert a RemoteEvent into ReplicatedStorage. Instead of using MoveTo to move the character, use SetPrimaryPartCFrame.

-- LocalScript

local button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CloneTool = ReplicatedStorage:WaitForChild("CloneTool")
local plr = button.Parent

button.MouseButton1Click:Connect(function()
    CloneTool:FireServer("Sword")
    script.Parent.Parent.Parent.ItemEquipper.Text = "EquippedTool = Sword"
end)

Server code:

-- Server script, in ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CloneTool = ReplicatedStorage:WaitForChild("CloneTool")

CloneTool.OnServerEvent:Connect(function(player, toolName)
    -- placed sword in server storage so it's secure
    local clone = game:GetService("ServerStorage")[toolName]:Clone()
    clone.Parent = player.Backpack
    player.Character:SetPrimaryPartCFrame(workspace.Tp.Position)
end)

Answer this question