How to access an individual player's workspace and not the server's workspace

I am spawning in parts from a LocalScript because I only want the individual player to be able to see those parts. I would like to put this in a module script so it can’t get exploited. I am trying to delete those parts as a part of a part reset. Thanks for any help :slight_smile:

Here is all the code for the part spawer (local script):

local plr = game:GetService("Players").LocalPlayer
local workspace = game:GetService("Workspace")
local rs = game:GetService("ReplicatedStorage")
local remoteFolder = rs:WaitForChild("Remotes")
local isStart = plr:WaitForChild("Data"):WaitForChild("isStart")
local isStopped = plr:WaitForChild("Data"):WaitForChild("isStopped")

function decreasePartTransparency(part)
	local targetTransparency = 0.9
	local transparencyTick = 0.01
	local duration = (0.9 - part.Transparency) / transparencyTick

	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)

	local transparencyTween = game:GetService("TweenService"):Create(part, tweenInfo, { Transparency = targetTransparency })

	transparencyTween:Play()
end

local partsFolder = Instance.new("Folder")
partsFolder.Parent = workspace
partsFolder.Name = "PartsFolder"

isStart.Changed:Connect(function()
	while task.wait() and isStart.Value == true and isStopped.Value == false do
		print("isStart")
		local part = Instance.new("Part")
		part.CastShadow = false
		part.BrickColor = BrickColor.new(Color3.new(0.00784314, 0.270588, 1))
		part.Transparency = 0.6
		part.Position = Vector3.new(math.random(-100, 100), math.random(110, 150), math.random(-100, 100))
		part.Parent = partsFolder
		remoteFolder:WaitForChild("AddPart"):FireServer(plr)

		decreasePartTransparency(part)
	end
end)

isStopped.Changed:Connect(function()
	while task.wait() and isStart.Value == true and isStopped.Value == false do
		print("isStopped")
		local part = Instance.new("Part")
		part.CastShadow = false
		part.BrickColor = BrickColor.new(Color3.new(0.00784314, 0.270588, 1))
		part.Transparency = 0.6
		part.Position = Vector3.new(math.random(-100, 100), math.random(110, 150), math.random(-100, 100))
		part.Parent = partsFolder
		remoteFolder:WaitForChild("AddPart"):FireServer(plr)

		decreasePartTransparency(part)
	end
end)

And here is all the code for the server-side OnRemoteEvent:

remoteFolder:WaitForChild("AddPart").OnServerEvent:Connect(function(player)
	player:WaitForChild("leaderstats"):WaitForChild("Parts").Value += 1
end)

remoteFolder:WaitForChild("StartParts").OnServerEvent:Connect(function(player)
	player:WaitForChild("Data"):WaitForChild("isStart").Value = true
end)

remoteFolder:WaitForChild("StopParts").OnServerEvent:Connect(function(player)
	player:WaitForChild("Data"):WaitForChild("isStart").Value = false
end)

remoteFolder:WaitForChild("ResetParts").OnServerEvent:Connect(function(player)
	print("ResetReq")
	player:WaitForChild("Data"):WaitForChild("isStopped").Value = true
	resetPartsModule.deleteParts()
	print("ResetFin")
	player:WaitForChild("Data"):WaitForChild("isStopped").Value = false
	**strong text**
	player:WaitForChild("Data"):WaitForChild("PartsValue").Value = 0
end)

I get the print 'ResetReq' but not 'ResetFin'

And here is the code for the module script

local module = {}

function module.deleteParts()
	local plr = game:GetService("Players").LocalPlayer
	for _, part in ipairs(game:GetService("Workspace"):WaitForChild("PartsFolder"):GetChildren()) do
		part:Destroy()
	end
end

return module

Make DeleteParts a remote event. Then fire (on server script) with:

remoteFolder:WaitForChild("DeleteParts"):FireClient(player)

next receive it on a local script with:

remoteFolder:WaitForChild("DeleteParts"):OnClientEvent():Connect(function()
	local plr = game:GetService("Players").LocalPlayer
	for _, part in ipairs(game:GetService("Workspace"):WaitForChild("PartsFolder"):GetChildren()) do
		part:Destroy()
	end
end

BTW you should not send “plr” when you are using FireServer as it automatically does this and you are just adding a parameter you are not using.

2 Likes

The ‘DeleteParts’ remote event is supposed to happen when you click on a button UI, so how can I detect that with a server script?

You would have to have the button delete the parts and send a remote event to the server to reset “isStopped”, and “PartsValue”. Sadly you can not edit someones workspace directly.

1 Like

Thanks for your help @TimeKillsTheWeak !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.