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

how can i enable a localscript (in starterplayerscripts) with a clickdetector button?

i'm using buildthomas' rain plugin, and i want to control if rain is on or not with a button inside workspace, i tried a few methods (as well as multiple other rain scripts), but none of them seemed to work.

the plugin puts a localscript in SPS, so what i'm trying to do is activate rain for all clients (RainScript.Disabled = false) in order to make the rain effect appear for everyone, as well as new players who join the game while its already raining

can i get any help with this? thanks!

1 answer

✓ Accepted answer

First, we have to put the localscript RainScript inside ServerStorage instead of StarterPlayerScripts. This is because any scripts inside StarterPlayerScripts will be moved to PlayerScripts once the player has joined the game, and the server cannot reference PlayerScripts.

Insert the following script inside the brick with the ClickDetector.

local players = game:GetService("Players")
local LegacyRainScript = game.ServerStorage:WaitForChild("RainScript")
local isRaining = false

game.Players.PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local ClonedRainScript = LegacyRainScript:Clone()
	ClonedRainScript.Parent = Character
	
	if isRaining then
		ClonedRainScript.Disabled = false
	end
end)

script.Parent.ClickDetector.MouseClick:Connect(function()
	isRaining = true
	for i, v in pairs(players:GetChildren()) do
		if v.Character.RainScript then
			v.Character.RainScript.Disabled = false
		end
	end
end)

If you would instead like the button to be able to toggle the rain on and off every time it's clicked for everybody, then replace it with this:

local players = game:GetService("Players")
local LegacyRainScript = game.ServerStorage:WaitForChild("RainScript")
local isRaining = false

game.Players.PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local ClonedRainScript = LegacyRainScript:Clone()
	ClonedRainScript.Parent = Character
	
	if isRaining then
		ClonedRainScript.Disabled = false
	end
end)

script.Parent.ClickDetector.MouseClick:Connect(function()
	isRaining = not isRaining
	for i, v in pairs(players:GetChildren()) do
		if v.Character.RainScript then
			v.Character.RainScript.Disabled = not v.Character.RainScript.Disabled
		end
	end
end)

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