I’m trying to make it to where when you go touch a part it changes the time to night in lighting.
What is the issue?
I wanted to make it local because in multiplayer it will make it night for everyone and it is really distracting when it suddenly becomes night mid game. But I’m not sure how.
What solutions have you tried so far?
I haven’t found much help on the creator hub/forums or google. I’ve also tried putting in startercharacter scripts and redirecting the object path to the one in the workspace but that also didn’t work.
SCRIPT:
local lighting = game:FindFirstChild("Lighting")
local Tween = game:GetService("TweenService"):Create(lighting, TweenInfo.new(2), {
ClockTime = 0
})
script.Parent.Touched:Connect(function(hit)
if hit.Parent == game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then
Tween:Play()
end
end)
I PUT THIS IN A SERVER SCRIPT WITH THE RUNCONTEXT SET TO “LOCAL”
If you are trying to find the player by any character touching a part on a local script, do
local players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
if hit.Parent ~= nil then
local character = hit.Parent
local player = players:GetPlayerFromCharacter(character)
end
end)
No I wasn’t trying to fire an event to the sever, or can I do it the other way around and fire an event to the client? (sorry I’m not that good at coding yet I’ve only learned from reading other peoples code.)
If you want everyone to experience the day/night cycle by someone touching a part, there is nothing wrong with firing from client to server as you said.
Instead of checking if the hit is exactly the humanoidRootPart of the character, you can make it check if it’s the character entirely if hit.Parent and hit.Parent == character then and not if hit.Parent == game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then
You will most likely have to add a debounce to fire the event only once since touching the part without a debounce will cause exhaustion with the events you have used up.
I solved the issue! All I had to do was change it to the character instead of the humanoidrootpart as juju_lebelge said. Here’s the code. I put in inside the part that you interact with!
`local lighting = game:FindFirstChild(“Lighting”)
local Tween = game:GetService(“TweenService”):Create(lighting, TweenInfo.new(2), {
ClockTime = 0
})
script.Parent.Touched:Connect(function(hit)
if hit.Parent == game.Players.LocalPlayer.Character then
Tween:Play()
end
end)`