Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

using .Touched with a localscript?

Asked by
Astralyst 389 Moderation Voter
7 years ago

I'm kinda abusing the idea of filteringenabled right now, basically, I want to disable this script for only the client and not the whole server.

Here's my script;

1script.Parent.Touched:connect(function(hit)
2player = game.Players:GetPlayerFromCharacter(hit.Parent)
3    if player and player:IsA("Player") then
4    player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
5    script.Disabled = true
6    end
7end)

I also tried;

1player = game.Players.LocalPlayer
2 
3script.Parent.Touched:connect(function(hit)
4    if player and player:IsA("Player") then
5    player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
6    script.Disabled = true
7    end
8end)

Neither of them works.

(keep in mind that this is a localscript, i tried using normalscript for this script and it worked fine. I just want to replicate the changes to only the client, not the whole server.)

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
7 years ago

Local scripts only run when they are a descendant of something that is individual to the player, such as their gui, character, backpack, etc. As a result, a local script that is inside of a part will not run.

To fix this, you can put the local script inside one of these things that are individual to the client, and do something like this:

1workspace["PartName"].Touched:Connect(function(hit)
2   if hit.Parent == game.Players.LocalPlayer.Character then
3      --do stuff
4   end
5end)

However, if you only edit the leaderstats with a local script, it will not update on the server, thus not updating their stat for everyone else. To fix this you can use a server script instead and keep track of all the players who have touched the part before, only letting them touch it once.

01local plrsWhoTouched = {}
02script.Parent.Touched:Connect(function(hit)
03   local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
04   if plr then
05      for _,v in pairs(plrsWhoTouched) do
06         if v == plr then return end
07      end
08      plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value + 1
09      table.insert(plrsWhoTouched,1,plr)
10   end
11end)
0
Thanks a ton! ~ Astralyst 389 — 7y
Ad

Answer this question