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

Need Assistance with Events/Functions, anyone able to help?

I've attempted for over an hour now, researching Roblox's help pages and other sources, and haven't found a reason to why this isn't working as planned. It's supposed to change the time of day for all users and it still continues to fail.

Here's the code for the slider I added to change the in-game time:

local textB = script.Parent
local frame = textB.Parent
local max = frame.Max
local fire = max.Fire
local bar = max.Bar
local as = Vector2.new(max.AbsoluteSize.X, max.AbsoluteSize.Y)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage:WaitForChild("TimeManagement")

fire.Changed:Connect(function()
	
	if fire.Value == true then
	
		local maxSize = as.X
		local size = bar.Size.X.Offset
		local num = 24 * (size / maxSize)
	
		textB.Text = math.floor(num) + 1
		
		local almostThere = math.floor(num) + 1
		
		local newTime = almostThere * 60
		
		remoteFunction:InvokeServer(newTime) -- Important Line
	end
	
end)

Here's the code for the Server Script that I thought would work:

game:GetService("Lighting")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local remoteFunction = ReplicatedStorage:WaitForChild("TimeManagement")

local function onTimeUpdate(newestTime)
	game:GetService("Lighting"):SetMinutesAfterMidnight(newestTime)
end

remoteFunction.OnServerInvoke = onTimeUpdate

2 answers

✓ Accepted answer

OnServerInvoke has a built-in Player argument. Here's my rewrite, enjoy!

-- THE FIRST SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TextBox = (script.Parent:IsA("TextBox") and script.Parent) or script:FindFirstAncestorOfClass("TextBox")
local Frame = (TextBox.Parent:IsA("Frame") and TextBox.Parent) or TextBox:FindFirstAncestorOfClass("Frame")
local Max = Frame:FindFirstChild("Max")
local Fire = Max:FindFirstChild("Fire")
local Bar = Max:FindFirstChild("Bar")

repeat
	wait()
until Max ~= nil and Fire ~= nil and Bar ~= nil

local AbsoluteSize = Vector2.new(Max.AbsoluteSize.X,Max.AbsoluteSize.Y)
local RemoteFunction ReplicatedStorage:FindFirstChild("TimeManagement")

repeat
	wait()
until RemoteFunction ~= nil

Fire:GetPropertyChangedSignal("Value"):Connect(function()
	if Fire.Value ~= true then
		return
	end
	local MaxSize = AbsoluteSize.X
	local Size = Bar.Size.X.Offset
	local Number = 24 * (Size / MaxSize)
	TextBox.Text = math.floor(Number) + 1
	local AlmostThere = math.floor(Number) + 1
	local NewTime = AlmostThere * 60
	RemoteFunction:InvokeServer(NewTime)
end)

-------------

-- THE SECOND SCRIPT (SERVER SCRIPT)

local Lighting = game:GetService("Lighting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteFunction = ReplicatedStorage:FindFirstChild("TimeManagement")

repeat
	wait()
until RemoteFunction ~= nil

RemoteFunction.OnServerInvoke = function(Player,MinuteAfterMidnight)
	Lighting:SetMinutesAfterMidnight(MinuteAfterMidnight)
end

1 comment

0

Despite updating my scripts using your rewrite, I'm still unable to reach the desired outcome.

You need a return for RemoteFunctions to work.

local function onTimeUpdate(newestTime)
    	game:GetService("Lighting"):SetMinutesAfterMidnight(newestTime)
	return
end

remoteFunction.OnServerInvoke = onTimeUpdate

However, this doesnt seem like something you'd want a RemoteFunction for. Why not use a remote event?

1 comment

0

Sadly enough, after changing to a remote event and furthering your ideas, I still haven't been able to change the time using the slider.

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