Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make Client side scripts such that the script should only apply to the client?

How do I make Client side scripts such that the script applies to the client, not the whole server?

Example Task (Give script based on this example) I want a part to go invisible whenever a player touches it, it should go invisible only for the client from whom the player touched, nobody else should see it invisible unless they touch it too

Please give examples of how to use it if its in a part or block and how to use it if its in other places.

3 answers

✓ Accepted answer

The following code will work when placed in a local script inside StarterGui (or other client-only services), considering your game is FE (experimental mode off):

local part = -- Reference your part here
local player = game.Players.LocalPlayer

part.Touched:Connect(function(hit)
	if hit.Parent.Name == player.Name then part.Transparency = 1 end
end)

Since your game is FE, the part turning transparent won't replicate to the server, therefor only the player who touched it will be able to see it turn invisible.

2 comments

0

But why does it only work if i put it in the starter gui, why no-where else?

0

It does work on other places such as StarterPack, ReplicatedStorage, StarterPlayerScripts, etc. Just make sure you place it somewhere the client can access.

You could try this:

--Server script
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local RE = game.ReplicatedStorage.RemoteEvent
RE:FireClient(game.Players:FindFirstChild(hit.Parent.Name,script.Parent)
end
end)

--Client Script
local RE = game.ReplicatedStorage.RemoteEvent
RE.OnClientEvent:Connect(function(part)
part.Transparency = 1
end)

(This is untested in studio)

Please reply if it works, or doesnt work :P

1 comment

0

This situation does not require remotes. Clients run physics too.

You would put this in a local script in the StarterPlayer --> StarterPlayerScripts

local part = Instance.new("Part")
part.Parent = game.Workspace
part.Name = "LocalPart"
part.Position = Vector3.new(70.395, 7.619, 61.615)
part.Size = Vector3.new(10.09, 10.8, 2)
part.Anchored = true

part.Touched:connect(function()
	part.CanCollide = false
	part.Transparency = .8
	wait(2)
	part.CanCollide = true
	part.Transparency = 0
end)

Change the position, name and the size to be what you want, you can manipulate the part however you want as well. Such as changing the color, rotation, and material. I just did a simple part.

This discussion is preserved as it was. New answers, comments, and votes are closed.