The event is fired on entering the game and not when Activating the tool

Ok, to explain it well, there is an event that is fired from server to client, which adds a random Strength value and is equal to the value of an IntValue, the event must be fired or executed when the player through the Activate event does click on a specific tool, everything works fine but the problem starts when the player enters the experience and apparently the value is added to the leaderstats even though the character had not even loaded and if you look closely you will notice that there is a little late the sum of the textlabel that appears on the player’s screen.

This is the script on ServerScriptService:

Script
local RS = game:GetService("ReplicatedStorage")
local GainStrength = RS.RemoteEvents.GainStrength

GainStrength.OnServerEvent:Connect(function(player)
	local StrengthValue = math.random(1, 10)

	player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + StrengthValue
	player.PlayerGui.GainStrengthGui.StrengthLabelFolder.StrengthFrame.StrengthLabel.Text = "+"..StrengthValue .." Strength"
end)

This is the local script in a tool on StarterPack:

LocalScript
WeightTool.Activated:Connect(function()
	local character = player.Character or player.CharacterAdded:Wait()
	if character then
		humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local AnimatorActivate = humanoid:FindFirstChildOfClass("Animator")
			if AnimatorActivate then
				AnimationTrackActivate = AnimatorActivate:LoadAnimation(StrengthAnimationActivate)
			   if AnimationTrackActivate then
				  if db == false then
					 db = true
					 AnimationTrackActivate:Play()
					 GainStrengthEvent:FireServer()
					 ImagesClones()
					 task.wait(1.2)
					 db = false
					 return AnimationTrackActivate
				  end
			   end
			end
		end
	end
end)
``

Problem:

As you can see, i started with 8 Strength in my leaderstats when i joined to the game, and no, I don’t have another script that makes it add the value of Strength when joining, there is no line of code that does the same either, the event is simply fired when entering and not when the tool is activated, honestly I don’t know what happens.

1 Like

Please post the whole LocalScript, and if that’s not the whole server script then please post that too.

BTW you can use the += operator to avoid repeating the variable, e.g. a = a + b is the same as a += b.

Of course, the server script is complete, heres the local:

--[Important Variables]
local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")
local GainStrengthEvent = RS.RemoteEvents.GainStrength
local player = game.Players.LocalPlayer
local WeightTool = script.Parent
local db = false
--[Animation Variables]
local StrengthAnimationEquip = script.Parent:WaitForChild("StrengthAnimationEquip", 0.5)
local StrengthAnimationActivate = script.Parent:WaitForChild("StrengthAnimationActivate", 0.5)
local AnimationTrackEquip
local AnimationTrackActivate
local humanoid
--[Gui Variables]
local GainStrengthGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("GainStrengthGui")
local StrengthImage = GainStrengthGui.StrengthLabelFolder.StrengthFrame.StrengthImageLabel
local StrengthLabel = StrengthImage.Parent.StrengthLabel
local StrenghtLabelStroke = StrengthLabel.UIStroke

function ImagesClones()
	local StrengthImageClone = StrengthImage:Clone()
	local StrengthLabelClone = StrengthLabel:Clone()
	StrengthImageClone.Parent = StrengthImage.Parent.Clones
	StrengthLabelClone.Parent = StrengthLabel.Parent.Clones
	
	local ClonesFolder = StrengthImage.Parent.Clones
	
	local Tweeninfo = TweenInfo.new(0, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)
	local ImagePosition = UDim2.fromOffset(math.random(25, 572), math.random(25, 345))
	local ImageLabelPosition = UDim2.fromOffset(ImagePosition.X.Offset + 27, ImagePosition.Y.Offset - 20)

	local Tween = TS:Create(StrengthImageClone, Tweeninfo, {Position = ImagePosition})
	local TweenLabel = TS:Create(StrengthLabelClone, Tweeninfo, {Position = ImageLabelPosition})

	local Tweeninfo2 = TweenInfo.new(2, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)
	local ImageRotation = 360

	local Tween2 = TS:Create(StrengthImageClone, Tweeninfo2, {Rotation = ImageRotation})

	local Tweeninfo4 = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	local ImageTransparency1 = 0
	local LabelTransparency = 0
	local StrokeTransparency = 0

	local Tween4 = TS:Create(StrengthImageClone, Tweeninfo4, {ImageTransparency = ImageTransparency1})
	local Tween5 = TS:Create(StrengthLabelClone, Tweeninfo4, {TextTransparency = LabelTransparency})
	local Tween6 = TS:Create(StrengthLabelClone.UIStroke, Tweeninfo4, {Transparency = StrokeTransparency})

	Tween:Play()
	TweenLabel:Play()
 	Tween2:Play()

	Tween4:Play()
	Tween5:Play()	
	Tween6:Play()
	
	local ImageTransparency2 = 1
	local LabelTransparency2 = 1
	local UiStrokeTransparency2 = 1
	local ImageRotation2 = 0
	local Tweeninfo5 = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.In, 0, false, 0)

	local Tween7 = TS:Create(StrengthImageClone, Tweeninfo5, {ImageTransparency = ImageTransparency2, Rotation = ImageRotation2})
	local Tween8 = TS:Create(StrengthLabelClone, Tweeninfo5, {TextTransparency = LabelTransparency2})
	local Tween9 = TS:Create(StrengthLabelClone.UIStroke, Tweeninfo5, {Transparency = UiStrokeTransparency2})
	
	Tween.Completed:Connect(function()
		task.wait(1)
		Tween7:Play()
		Tween8:Play()
		Tween9:Play()
		task.wait(1)
		if ClonesFolder:FindFirstChild("StrengthImageLabel") then
			StrengthImageClone:Destroy()
		end
		if ClonesFolder:FindFirstChild("StrengthLabel") then
			StrengthLabelClone:Destroy()
		end
	end)
end

WeightTool.Equipped:Connect(function(mouse)
	local character = WeightTool.Parent or player.CharacterAdded:Wait()
	if character then
		humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local AnimatorEquip = humanoid:FindFirstChildOfClass("Animator")
			if AnimatorEquip then
				AnimationTrackEquip = AnimatorEquip:LoadAnimation(StrengthAnimationEquip)
			   if AnimationTrackEquip then
				  AnimationTrackEquip:Play()
				  humanoid.UseJumpPower = true
				  humanoid.JumpPower = 0
				  humanoid.WalkSpeed = 0
				  return AnimationTrackEquip
			   end
			end
		end
	end
end)

WeightTool.Activated:Connect(function()
	local character = player.Character or player.CharacterAdded:Wait()
	if character then
		humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local AnimatorActivate = humanoid:FindFirstChildOfClass("Animator")
			if AnimatorActivate then
				AnimationTrackActivate = AnimatorActivate:LoadAnimation(StrengthAnimationActivate)
			   if AnimationTrackActivate then
				  if db == false then
					 db = true
					 AnimationTrackActivate:Play()
					 GainStrengthEvent:FireServer()
					 ImagesClones()
					 task.wait(1.2)
					 db = false
					 return AnimationTrackActivate
				  end
			   end
			end
		end
	end
end)

WeightTool.Unequipped:Connect(function()
	if AnimationTrackEquip ~= nil then
	end
	if AnimationTrackActivate ~= nil then
	   AnimationTrackActivate:Stop()
	end
	humanoid.UseJumpPower = true
	humanoid.JumpPower = 50
	   AnimationTrackEquip:Stop()
	humanoid.WalkSpeed = 16
end)

I tried that operator but still not working

1 Like

LocalScript looks fine, can’t spot anything that’d cause the issue.

To narrow the issue down, place a breakpoint in the LocalScript on this line: GainStrengthEvent:FireServer(), and also one in the ServerScript on this line: local StrengthValue = math.random(1, 10).

Test the game again, and see when each script pauses. If the LocalScript pauses when the game starts up, then something is somehow causing the Tool to be activated. If that doesn’t happen, but the ServerScript still pauses when the game starts, then some other LocalScript is firing the RemoteEvent. If neither script pauses when the game starts, and only when the player actually Activates the Tool, then some other server script is modifying the leaderstats value andthe player GUI stuff.

Ok, i already tried to place a breakpoint on both lines but im stuck with this screen:

Heres the lines idk if i did well, but since i started i have not errors:


1 Like

You’re doing it right. When execution reaches a line with a breakpoint it pauses. If you open a script while execution is paused, you can use the Debugger controls (to the right of the red Stop button) to step through the code one line at a time. The point in this case is just to see if those lines of code actually run at the start of the game, or if something else is causing the problem.

Do the scripts pause when the game starts, or only when you activate the tool by clicking?

Excuse me, i think i dont have debugger controls enabled

1 Like

Are they greyed out? I’m pretty sure you can’t turn them off with a setting or anything like that, but they’re only active when execution is halted at a breakpoint and when a script is open, I believe. It might be that Studio has to be in the same client/server context as the halting script, which you can toggle using the button to the right of the Play button that say Current: Server.

If all this is too complicated you could also just put some print statements instead of the breakpoints, that would work fine in this situation ^.^

Sorry, Debugger controls are enabled but i dont see them

1 Like

Oh i see, sorry i didnt knew that i need to enter the scripts

1 Like

Well, I couldn’t tell you what causes it to fire, based on experimentation I did about placing a breakpoint only on the line where the event fired on the client, it still fired, so it’s possible that the event was triggered. fire via other alternate scripts

1 Like

Another thing that I forgot to mention is that the tool inside the player is not the only means by which I trigger the event, I also do it for models but these are triggered when they are in a specific seat and when you click through the Button1Down event. They are local scripts that are inside the StarterCharacterScripts folder.

1 Like

If you want to get an overview of what scripts fire the event, try changing the name so the scripts will look for it but throw an error when they can’t find it because they use the old name. Or you can press Ctrl+Shift+F to search through all scripts in the game.

I will try it, i wanna show another local script of a model that uses the same RemoteEvent, is on the StarterCharacterScripts but idk if makes a difference:

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local GainStrength = RS.RemoteEvents.GainStrength
local ProximityPrompt = game.Workspace.WeightModel.Seat.ProximityPrompt
local player = game.Players.LocalPlayer
local WeightTool = ProximityPrompt.Parent.Parent.Handle
local WeightModel = ProximityPrompt.Parent.Parent
local seat = ProximityPrompt.Parent
local WeightAnimationSystemAction = ProximityPrompt:WaitForChild("WeightAnimationSystemAction")
local Character
local Humanoid
local db = false
local PlayerMouse = player:GetMouse()
seat.Position = Vector3.new(-1.452, 3.792, -41.78)

local AnimationTrackEquip
local AnimationTrackActivate
local connection

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant ~= nil then
		connection = PlayerMouse.Button1Down:Connect(function()
				Character = player.Character or player.CharacterAdded:Wait()
				if Character then
					Humanoid = Character:FindFirstChild("Humanoid")
					if Humanoid then
						local AnimatorActivate = Humanoid:FindFirstChild("Animator")
						if AnimatorActivate then
							AnimationTrackActivate = AnimatorActivate:LoadAnimation(WeightAnimationSystemAction)
							if AnimationTrackActivate then
								if db == false then
									db = true
									AnimationTrackActivate:Play()
									GainStrength:FireServer()
									task.wait(1)
									db = false
								end
							end
						end
					end
			else
				connection:Disconnect()
			end
		end)
	end
end)

seat.ChildRemoved:Connect(function(Child)
	if connection then
		connection:Disconnect()
	end
	local Character = player.Character or player.CharacterAdded:Wait()
	if Character then
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if Humanoid then
			Humanoid:MoveTo(game.Workspace.PartV1.Position)
		end
	end
	if AnimationTrackActivate ~= nil then
		AnimationTrackActivate:Stop()
	end
end)

This is the complete local script, the other models have the same code

Oh im so dumb, thank you what works perfectly, i knew that any of my scripts works fine but i had this one that makes difficult the search:

local RS = game:GetService("ReplicatedStorage")
local GainStrengthEvent = RS.RemoteEvents.GainStrength
local StrengthValueLabel = script.Parent
local player = game.Players.LocalPlayer
local StrengthValue = player.leaderstats.Strength

local Abbreviations = {
	No = 10^30;
	Oc = 10^27;
	Sp = 10^24;
	Sx = 10^21;
	Qi = 10^18;
	Qa = 10^15;
	T = 10^12;
	B = 10^9;
	M = 10^6;
	K = 10^3
}

GainStrengthEvent:FireServer(StrengthValue)

function abbreviateNums(number)


	local abbreviatedNum = number
	local abbreviationChosen = 0


	for abbreviation, num in pairs(Abbreviations) do

		if number >= num and num > abbreviationChosen then

			local shortNum = number / num
			local intNum = math.floor(shortNum)

			abbreviatedNum = tostring(intNum) .. abbreviation
			abbreviationChosen = num
		end
	end

	return abbreviatedNum
end

StrengthValueLabel.Text = abbreviateNums(StrengthValue.Value)

StrengthValue.Changed:Connect(function()
	StrengthValueLabel.Text = abbreviateNums(StrengthValue.Value)
end)

I fired the event from this one when i enter

1 Like

Alright, glad I could help ^.^

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