My local script is editing everyone's attributes based on a single person's action

I’m working on a local script that allows players to drink water when they enter a specific zone. I’m using the ZonePlus module script to detect when players enter the zone.

Current Behavior:

  • Before any player enters the zone:


  • When Player 1 enters the zone: canDrink is set to true for both Player 1 and Player 2.

  • When Player 2 (not in the zone) presses E: Player 2 starts drinking because canDrink is true.

Problem:

My attribute values are being edited globally, affecting all players instead of just the one that entered the zone.

Code:

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = workspace.DrinkAreas
local DrinkZones = Zone.new(container)

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local TL = script.Parent
local thing = TL.Parent

local t = {thing, TL, thing.Text}

function tween(r, s)
    local info = TweenInfo.new(s, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
    for _, item in ipairs(t) do
        if item.Name == "Text" then
            TweenService:Create(item, info, {TextTransparency = r}):Play()
        elseif item.Name == "TextLabel" then
            TweenService:Create(item, info, {Transparency = r}):Play()
        elseif item.Name == "Drink" then
            TweenService:Create(item, info, {ImageTransparency = r}):Play()
        end
    end
end

DrinkZones.playerEntered:Connect(function(plr: Player)
    thing:SetAttribute("canDrink", true)
    print(("%s entered the zone!"):format(plr.Name))
    tween(0, 0.3)
end)

DrinkZones.playerExited:Connect(function(player)
    thing:SetAttribute("canDrink", false)
    thing:SetAttribute("isDrinking", false)
    print(("%s exited the zone!"):format(player.Name))
    tween(1, 0.3)
end)

local function startDrinking()
    thing:SetAttribute("canDrink", false)
    local plr = game.Players.LocalPlayer
    local hum = plr.Character:WaitForChild("Humanoid")
    local anim = hum:LoadAnimation(script.Animation)
    anim.Looped = false
    print(plr.Name)
    
    while thing:GetAttribute("isDrinking") do
        anim:Play()
        anim.Stopped:Wait()
        plr:SetAttribute("Thirst", plr:GetAttribute("Thirst") + 10)
        wait(0.55)
    end
    thing:SetAttribute("canDrink", true)
end

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E and thing:GetAttribute("canDrink") and not thing:GetAttribute("isDrinking") then
        TweenService:Create(workspace.Camera, TweenInfo.new(3), {FieldOfView = 30}):Play()
        tween(1, 0.3)
        
        thing:SetAttribute("isDrinking", true)
        print("Started drinking...")
        startDrinking()
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E and thing:GetAttribute("isDrinking") then
        TweenService:Create(workspace.Camera, TweenInfo.new(3), {FieldOfView = 70}):Play()
        tween(0, 0.3)
        thing:SetAttribute("isDrinking", false)
        print("Stopped drinking.")
    end
end)

The issue is that every player has the same script so when that event fires every player will set their attribute. Switch it to this:

DrinkZones.playerEntered:Connect(function(plr: Player)
	if game:GetService("Players").LocalPlayer ~= plr then return end

	thing:SetAttribute("canDrink", true)
	print(("%s entered the zone!"):format(plr.Name))
	tween(0, 0.3)
end)

DrinkZones.playerExited:Connect(function(player)
	if game:GetService("Players").LocalPlayer ~= player then return end
	
	thing:SetAttribute("canDrink", false)
	thing:SetAttribute("isDrinking", false)
	print(("%s exited the zone!"):format(player.Name))
	tween(1, 0.3)
end)
1 Like

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