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
6 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;

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

I also tried;

player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(hit)
    if player and player:IsA("Player") then
    player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
    script.Disabled = true
    end
end)

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
6 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:

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

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.

local plrsWhoTouched = {}
script.Parent.Touched:Connect(function(hit)
   local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
   if plr then
      for _,v in pairs(plrsWhoTouched) do
         if v == plr then return end
      end
      plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value + 1
      table.insert(plrsWhoTouched,1,plr)
   end
end)
0
Thanks a ton! ~ Astralyst 389 — 6y
Ad

Answer this question