How can I detect if a part is touched by the player on a local script?

  1. What do you want to achieve?

I’m trying to make it to where when you go touch a part it changes the time to night in lighting.

  1. 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.

  1. 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)

Are you using a remote event from a local script to the server?

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.

That is the opposite of what I was trying to do. But I’ll try something.

So you want the day/night cycle to affect all players using a server script? I don’t understand the issue that you are trying solve.

Was it when you used tween service, it never worked as intended?

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)`

Yeah, sorry I was never any help at all. Glad it worked.

No problem. You kinda did help give some ideas for a fix!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.