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

How do I make a script in workspace work for only 1 player?

Asked by 6 years ago

Hey there, I've recently tested my place and came across a problem: I've made a script that changes the outdoorambient and fogstart etc. for a cave. When I tested it with my friend both our outdoorambients got changed when only 1 person touched it, and the other was on the other side of the map.

I've tried using localscripts but then the script doesn't do anything, and I can't find anything on the wiki. Does anyone know how to solve this?

By the way here's the code I used to change the outdoorambient:

local lighting = game.Lighting
local Debounce = false
local Speed = 1

local function Tween(t,s,d,p,o)
    return game:GetService("TweenService"):Create(o,TweenInfo.new(t,Enum.EasingStyle[s],Enum.EasingDirection[d]),p)
end

script.Parent.Touched:Connect(function()
    if not Debounce then 
        Debounce = true
        Tween(Speed,"Sine","InOut",{Brightness = 1,FogStart = 0,FogColor = Color3.fromRGB(191, 191, 191),OutdoorAmbient = Color3.fromRGB(127, 127, 127)},lighting):Play()
Debounce = false
    end
end)
0
what? Theroofypidgeot 21 — 6y

2 answers

Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
6 years ago

You were on the right path with using a LocalScript. For something to affect only one client, it usually has to happen only on that client.

The issue with your attempt to use a LocalScript was probably the location of it; LocalScripts only run if they are a descendant of:

With this in mind, I recommend creating a LocalScript inside StarterPlayerScripts that changes the lighting when the part is touched. Of course, you'll have to make an absolute path to the part instead of a relative one, but this doesn't really matter.

Ad
Log in to vote
0
Answered by 6 years ago

As this is a effect only it would need to be a local script so that the change is only local to that player. Secondly you would need to exclude other players as the event will run for anything that touched the part.

Local scripts do not run in the workspace so simple chaning the script to a local script will not work. I would place the script in StarterPlayerScripts then wait for the part in the workspace to be loaded in.

If you are planning of having multiple caves I would create a module to handle the setup as they will all share the same code but different cariables.

Lastly you are not waiting for the tween to complete making your debounce redundant. Use the event Completed

Example

-- please use get service in local script 
local plrServ = game:GetService("Players")
local lighting = game:GetService("Lighting")
local tweenServ = game:GetService("TweenService")

local localPlr = plrServ.LocalPlayer
local deb = false
local speed = 1
local tweenInfo = TweenInfo.new(speed, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) 

local part = [location to the part ie workspace:WaitForChild("Part")]

local function isLocalPlayer(hit)
    local plr = plrServ:GetPlayerFromCharacter(hit.Parent)
    return plr and plr == localPlr 
end

part.Touched:Connect(function(hit)
    if deb then return end -- deb check
    if not isLocalPlayer(hit) then return end -- only run for local player

    deb = true
    local tween = tweenServ:Create(lighting,tweenInfo, {Brightness = 1,FogStart = 0,FogColor = Color3.fromRGB(191, 191, 191),OutdoorAmbient = Color3.fromRGB(127, 127, 127)})

    tween:Play()
    tween.Completed:Wait() -- wait for tween to finish
    deb = false
end)

hope this helps.

Answer this question