ServerScript won't work if Cloned

So currently, I have a script that when you touch a specific item, it’ll remove it from the map and change a value. But I have it within a map that I clone from ReplicatedStorage.

This script works if I just leave the map in Workspace, but for whatever reason, it’ll break if I clone the map.

It’s probably something simple that I’m forgetting…but I have no idea why this won’t work.


ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OrbTouched = ReplicatedStorage.RemoteEvents:WaitForChild("OrbTouched")
local Debounce = false

for _, Item in pairs(script.Parent:GetChildren()) do
    if Item:IsA("BasePart") then
	    Item.Touched:Connect(function(hit)
		    local Character = hit:FindFirstAncestorOfClass("Model")
            if not Character then 
	            return 
            end

            local Player = game.Players:GetPlayerFromCharacter(Character)

            if not Player then 
        	    return
            end	
            if not Debounce then
	            Debounce = true
                OrbTouched:FireClient(game.Players[Character.Name], Item.Name, script.Parent.Parent.Name)--Which gets the levelName) 
            end
            Debounce = false
	    end)
    end
end

Can you show us the code that clones the map? If it’s being cloned locally (in a LocalScript), the server script won’t be able to run because it won’t exist on the server.

Ah. Yeah, being cloned by a LocalScript…

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LoadLevel = ReplicatedStorage.RemoteEvents:WaitForChild("LoadLevel")

LoadLevel.OnClientEvent:Connect(function(LevelValue, PlayerName)
	if game.Workspace:FindFirstChild("Spawn") ~= nil and game.Workspace:FindFirstChild(LevelValue) == nil then
		print(LevelValue)
		local LevelToLoad = ReplicatedStorage.Levels[LevelValue]:Clone()
    	LevelToLoad.Parent = workspace
    	wait(1)
    	game.Players[PlayerName].Character:MoveTo(workspace[LevelValue]["Spawn"].Position)
    	game.Workspace.Spawn:Destroy()
    end
end)

Yeah, if a server script is cloned by a LocalScript it won’t run. You’ll have to clone it on the server or find some way to use a LocalScript for that.

Ah. Yeah I don’t want to clone it on the Server because I only want the one player to see it. Any idea on how to make this work locally?

I can’t really wrap my head around what you’re trying to accomplish.

But your implementation is broken by design. Just change your approach. Figure out how you can have action X run only on the client. You can mainly achieve this… by running whatever is in your server script… locally on the client. It’ll still modify the map and change a value. Only the server won’t see this value.

If you want the server to see this value. Then you’ll have to again change your approach to the problem.

1 Like

You could just loop through the children within the local script that clones it. It’s not the cleanest solution, but it’s the simplest and should work fine. You’ll just have to switch to BindableEvents to tell another localscript the item and level name (or whatever it is you’re firing, not sure). It’d look something like this instead for the FireClient part:

OrbTouched:Fire(Item.Name, Item.Parent.Parent.Name)

And to receive the event:

OrbTouched.Event:Connect(function(itemName, levelName)
     -- whatever
end)

With bindables, you don’t need the client as the first argument since it’s just client to client communication. Since the script wouldn’t be directly inside the thing you’re looping through, you’d have to access it some other way.

1 Like

I’ll try this first…If I can’t get it to work, I’ll switch to a new approach as @T0ny said.

Thank you both. :slight_smile:

2 Likes

Eyy! I’ve got it working now. :slight_smile:

Thank you so much!

as I type this, I realize I did pretty much what @T0ny said. xd

1 Like