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

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

Asked by 6 years ago

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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.

0
But why does it only work if i put it in the starter gui, why no-where else? AlexStorms 21 — 6y
0
It does work on other places such as StarterPack, ReplicatedStorage, StarterPlayerScripts, etc. Just make sure you place it somewhere the client can access. Le_Teapots 913 — 6y
Ad
Log in to vote
0
Answered by
Nikkulaos 229 Moderation Voter
6 years ago
Edited 6 years ago

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

0
This situation does not require remotes. Clients run physics too. cabbler 1942 — 6y
Log in to vote
0
Answered by 6 years ago

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.

Answer this question