Hi there, is there a way to run the function without using Players.PlayerAdded:Connect because I don’t want to run the code when player joins. It runs the code wherever player touch the part. Use localscript to run won’t work becaause it have some datas and table in the script.
Any help would be greatly appreciated👏
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Players = game:GetService(“Players”)
local remoteEvent = ReplicatedStorage:WaitForChild(“RemoteEventTest”)
from what i understand is that you dont want to use PlayerAdded to fire the client but to fire the client whenever they touch a part. well you can just simply add a script to the part then add these lines:
local Remote = ReplicatedStorage:WaitForChild(“RemoteEventTest”)
local part = script.Parent
part.Touched:Connect(function(Part)
if game.Players:GetPlayerFromCharacter(Part.Parent) then
local plr = game.Players:GetPlayerFromCharacter(Part.Parent)
Remote:FireClient(plr)
end
end)
yeah make it a server script inside the part
This using the Touched same as Mrchips script tho but i let the format here for you to be creative with it
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
--insert code
end
end)
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RE = RS:WaitForChild("RemoteEventTest")
local Part = script.Parent
local Debounce = false
Part.Touched:Connect(function(Hit)
if Debounce then
return
end
if Players:GetPlayerFromCharacter(Hit.Parent) then
Debounce = true
local Player = Players:GetPlayerFromCharacter(Hit.Parent)
RE:FireClient(Player)
task.wait(5) --cooldown
Debounce = false
end
end)
Added a debounce to prevent the connected callback function from being executed too frequently.