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

I'm trying to spawn stuff from a folder in ReplicatedStorage and have it go to workspace?

Asked by 5 years ago

Alright so, I'm trying to make it so I can spawn stuff from a folder in ReplicatedStorage to Workspace but only if it doesn't find the model I want to spawn in. I am fairly new to this scripting thing so I might have some issues. It would be spawned and despawned through a GUI TextButton. The function i'm using is called SpawnFunction. Script inside the ServerScriptStorage [Typical Script]

ReplicatedStorage = game.ReplicatedStorage
workspace = game.Workspace -- This is just extra insurance 
--Made By MillerrIAm
---------------------------
--Start Script
--Main Script
    if player,spawn true then
        workspace:GetChildren(spawn) then
        workspace:(spawn):Destroy()
    else
        ReplicatedStorage.Stipulation:Clone(spawn).Parent = workspace
    end

Script inside the GUI Button [Local Script]

--Made By MillerrIAm
---------------------------
--Start Script
--Main Script
function onClick()
game.ReplicatedStorage.SpawnFunction:InvokeServer("SpawnMe!") --Ignore the name, that's just what i named the model.
end

script.Parent.MouseButton1Down:connect(onClick)
print("GUIButton Working")
1
Bro i have a feeling u havent learned the basics from this line right here workspace:(spawn):Destroy() The_Pr0fessor 595 — 5y
0
I know the basics and I kind of figured that was wrong but I have no idea what else i'm suppose to do. Just2Terrify 566 — 5y

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
5 years ago
Edited 5 years ago

For starters, you don't need to reference the workspace hardly ever in a variable. The global workspace is the quickest and best way to access the workspace.

Anyway, you want to say "The client wants a map from clicking this button, and the server wants to clone it into the workspace" right?

So first, you'd insert a local script into your text button. Then you'd store the replicated storage and a remote event in some variables.

--Client

--local script inside button

local replicatedStorage  = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local button = script.Parent

local function onClick()
    remoteEvent:FireServer()
end

button.MouseButton1Down:Connect(onClick)

--Server

--server script

local replicatedStorage  = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local debounce = false
local function addMaps()
    if not debounce then
        debounce = true
        local map = replicatedStorage:WaitForChild("Map"):Clone() --Clone the map (should be a model)
        map.Parent = workspace --parent to workspace
    else
        workspace:FindFirstChild("Map"):Destroy() --say you wanted to delete it if you pressed again
        debounce = false
    end
end

remoteEvent.OnServerEvent:Connect(addMaps)

90% sure this is a troll question but maybe you'll learn something. Have at it.

1
I wish it was a troll question. Just2Terrify 566 — 5y
0
Bahaha goodluck man Psudar 882 — 5y
0
workspace is not the quickest. It's a global. The quickest would be something like local workspace = workspace, but that's only really optimization worth doing if you're using the global in a tight loop or something. User#26971 0 — 5y
0
Haha, you're only saving like 0.003 seconds. The amount of time it takes to type "local" would never actually be recovered through time saved. The only time it would matter is if you localized the variable to a function or something, which is pretty stupid to have a variable for something like workspace. Its redundant. Psudar 882 — 5y
Ad

Answer this question