So, I have a LocalScript in StarterCharacterScripts that turns the part to red when it is touched.
I know that LocalScripts run on the client only.
I tested my game by controlling two players in the game.
I touched the part with Player1, and it turned red on both Player1’s device and Player2’s device. It turned red on Player2’s device, even tho Player2 didn’t touch the part.
Here’s the LocalScript
local part1 = game.Workspace:WaitForChild("Part1")
part1.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
part1.BrickColor = BrickColor.new("Really red")
end
end)
Do I not understand LocalScripts correctly?
5 Likes
You should check if hit
is in the LocalPlayer’s character
1 Like
here I will update your thing mate
local lplr=game:GetService("Players").LocalPlayer
local part1 = workspace:WaitForChild("Part1")
part1.Touched:Connect(function(hit)
local char=hit:FindFirstAncestorOfClass("Model")
if lplr.Character==nil then
return
end
if char~=lplr.Character then
return
end
if char:FindFirstChild("Humanoid") then
part1.BrickColor = BrickColor.new("Really red")
end
end)
1 Like
Thank you so much mate:))))))))
Maybe this would work
local partOne = game.Workspace:WaitForChild("Part1")
partOne.Touched:Connect(function(hit)
local char = nil
if hit and hit.Parent then char = hit.Parent else return end
if game.Players:GetPlayerFromCharacter(char) then
part1.BrickColor = BrickColor.new("Really red")
end
end)
if you wonder, if you try it in normal script it should work.
1 Like
OP wants it to be local so the part turns red for only one player.
1 Like
This one seems complicated to me.
I just wanna know why my script doesn’t work.
1 Like
I am not sure but I think it is because the LocalScript isn’t checking what player touched it, therefore it doesn’t appear for anyone. My script is basically the same as yours but it first checks if “hit” (what touched Part1) and its parent exists. Then, it makes a variable named “char” to store the parent of whatever touched the part.
The last step is using the function:
:GetPlayerFromCharacter()
in Players to check if whatever is stored in “char” matches up with the actual player.
Another method you could use is this.
local partOne = game.Workspace:WaitForChild("Part1")
local player = game.Players.LocalPlayer -- Stores the current Player in a variable (local)
partOne.Touched:Connect(function(hit)
local char = nil -- Defines the "char" variable (set to nil currently)
if hit and hit.Parent then char = hit.Parent else return end -- If the part and its parent exists, continue, otherwise exit the function.
if player.Character == char then --Checks if the current player's character is equal to what is stored in the char variable
part1.BrickColor = BrickColor.new("Really red")
end
end)
1 Like
I actually understand it now, but not entirely.
I might use ur script
Thank you!
second solution for the issue would be something like this
server script in ServerScriptService
local remote=Instance.new("RemoteEvent")
remote.Name="Part1Touched"
remote.Parent=game:GetService("ReplicatedStorage")
local part1 = workspace:WaitForChild("Part1")
part1.Touched:Connect(function(hit)
local char=hit:FindFirstAncestorOfClass("Model")
local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
if typeof(plr)~="Instance" then
return
end
remote:FireClient(plr)
end)
LocalScript in ReplicatedFirst
local part1 = workspace:WaitForChild("Part1")
local remote=game:GetService("ReplicatedStorage"):WaitForChild("Part1Touched")
remote.OnClientEvent:Connect(function()
part1.BrickColor = BrickColor.new("Really red")
end)
3 Likes
that’s too complicated for something simple like this lol but it’s really good
1 Like
This one seems more complicated, but thank you so much for taking the time:)
1 Like
I might either use your script or make a simpler fix.
I might add a button that teleports the player to another place instead of making the events in the main game happen for the client only.
But thank you for explaining:)
1 Like
You’re welcome! I hope I helped
1 Like