Issues - fire server don't work!

Hello! I have a problem in my event server, the prints are not shown, it doesn’t work…
idk what’s wrong :frowning:

local script:



-- Services and Check
local inputService = game:GetService("UserInputService")
local replicatedStorageEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events")
local buttonPressed = false

-- Tool Variables
local snowBallTool = game.ReplicatedStorage:WaitForChild("tools"):FindFirstChild("Snowball")
snowBallTool.RequiresHandle = false
snowBallTool.Parent = game.Players.LocalPlayer.Backpack

-- Tool Function
function onActivation()	
	
	-- pressed
	inputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print('Btn 1 - Pressed')
			replicatedStorageEvent.snowBall_Event:FireServer()
			buttonPressed = true
			wait(.1)
		end
	end)
	
	-- stop pressed
	inputService.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print('Btn 1 - Stopped')
			buttonPressed = false
			wait(.1)
		end
	end)
end

-- Tool Activated
snowBallTool.Activated:Connect(onActivation)

server script:

local hum

game.Players.ChildAdded:Connect(function(plr)
	while true do wait()
		if plr.Character then break end
	end
	hum = plr.Character:WaitForChild("Humanoid")
end)

-- Services
local replicatedStorageEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events")

-- Anim Variables
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=11386848572" -- Roblox dance emote

local animationTrack = hum:LoadAnimation(animation)

-- Fire Event
replicatedStorageEvent.snowBall_Event.OnServerEvent:Connect(function()
	print('event fired')
	-- play anim throw snow ball
	animationTrack:Play()
	print('anim played')
	-- hit another player and deal dmg
end)

ok, I see a lot of issues in this, but I’ll start with why the prints aren’t showing:

  • you may have some Infinite yield possible on 'something' or a syntax error somewhere or you may have messed up using :FindFirstChild and got nil
    • check for errors and warnings in the output and adjust your explorer or script for the message
  • maybe you placed the tool into the backpack too early. Roblox is weird and clears your backpack a little bit after the character spawns deleting anything you put in it earlier
  • you’re connecting to InputBegan and InputEnded each time snowBallTool is activated
    • so the first time you activate the tool, it will visually do nothing and connect the 2 functions
    • Then when you activate the tool the second time, it should print 'Btn 1 - Pressed', but it will also connect the 2 functions again
    • having those functions connected again means that the third time you click, it will print 'Btn 1 - Pressed' twice at the same time. it will also connect a third pair
    • you should use .Activated to detect when the button is pressed and .Deactivated to detect when the button is released without UserInputService
  • the local script might also not be working if you placed it somewhere local scripts don’t work, like ServerStorage
    • there’s info here for where to place local scripts

then for the other issues:

  • you should set the .RequiresHandle property on the tool outside of the script. it’s in the properties window
  • don’t set the parent of the tool with a local script
    • either parent it with a server script, or leave the tool somewhere like StarterPack or StarterGear to be automatically given to the player
  • when you’re debugging with prints, add more prints
    • you should have one to show that the script is running at all outside of any functions
    • you should have one to show that functions are triggering when they should, so put one outside of both if statements in the local scripts
  • your buttonPressed variable does nothing
    • it looks like you were trying to setup a debounce so that players can’t spam left click, but you need an if statement to handle checking if buttonPressed=true to not start the snowball event
  • your stop pressed function does nothing

serverside issues:

  • you need to load the animation for each player, and for each of their characters
    • you’re currently loading it for hum which isn’t defined yet
    • hum gets defined when a player joins and spawns, but your line local animationTrack = hum:LoadAnimation(animation) doesn’t wait for that
      • this should be giving you an error in the output
    • to load the animation for each player and for each of their characters, you’d pretty much do this:
      local snowballAnimationTracksForEachPlayer = {}
      local function onPlayerAdded(playerThatWasAdded)
         local function onCharacterAdded(characterThatWasAdded)
            -- load the track and save it into the table for this player
            snowballAnimationTracksForEachPlayer[playerThatWasAdded] = ...
         end
      end
      
      local function onSnowBallEvent(playerThatFiredTheEvent)
         -- maybe setup a debounce here
         snowballAnimationTracksForEachPlayer[playerThatFiredTheEvent]:Play()
      
         -- throw a snowball
      end
      
      game.Players.PlayerAdded:Connect(onPlayerAdded)
      <the remote event>.OnServerEvent:Connect(onSnowBallEvent)
      
  • don’t use .ChildAdded to detect players being added, it may misfire for things like Parts being added into game.Players
    • use game.Players.PlayerAdded
  • use .CharacterAdded of a Player instance instead of having an infinite loop for the character
4 Likes

It’s the best answer I haven’t received in a long time, thank you very much! I am really learning from my mistakes and this is part of my learning, thanks for your feedback. :slight_smile:

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