Bug on my game I need to fix (but idk how)

  1. What do you want to achieve? Keep it simple and clear!
    -I want players in my game to be able to choose 1 out of 3 cards in a popup deck when it shows up (Roguelike kinda) and they can use that card as an ability.

  2. What is the issue? Include screenshots / videos if possible!
    -Whenever 2 or more people choose the same card, if one uses the ability, it also activates to the other person with the same card.

Local Script:

local player = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
	script.Parent.Parent.Parent.Parent.Enabled = false
	game:GetService("ReplicatedStorage").Remotes.CardPick:FireServer(script.Parent)
end)

Server Script:

local uis = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")
local tweenservice = game:GetService("TweenService")

game:GetService("ReplicatedStorage").Remotes.CardPick.OnServerEvent:Connect(function(player : Player, card : TextButton)
	local spawnedcards = player.PlayerCardSpawn
	local character = player.Character or player:WaitForChild("Character")
	local humanoid = character:FindFirstChild("Humanoid")
	local humanoidrootpart = character:FindFirstChild("HumanoidRootPart")

	game:GetService("ReplicatedStorage").Remotes.CardActivated.OnServerEvent:Connect(function()
		if abilityname.Value == "QuickFeet" and humanoid.Health > 0 then

			print(card.Name .. " ability activated")
			local startsound = replicated.Sounds.SpeedBoostStart:Clone()
			local endsound = replicated.Sounds.SpeedBoostEnd:Clone()
			startsound.Parent = character:FindFirstChild("HumanoidRootPart")
			endsound.Parent = character:FindFirstChild("HumanoidRootPart")

			humanoid.WalkSpeed += 13
		end
	end)
end)
1 Like

2 players select the same card
there is a connection to the cardactivated remote event for both of them
1 player activates the card

connects for both, and there isn’t any check on which player fired it, so both players get the boost. hopefully a better scripter than me can tell you how to actually fix it…

also why do you repeatedly get replicatedstorage? you made a variable for a reason!

local replicated = game:GetService("ReplicatedStorage")

the code in general is a bit iffy, like what is abilityname?

sorry i just started scripting (5-7 months) so idrk how do do all this stuff i just tried making my own system, abilityname (i removed the var in this version) is the name of the ability and so that i can check if they used the right ability, but i dont get what ur saying on that first statement

Why are you connecting event inside itself?
… dude what are you even doing? :skull:

local uis = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tweenservice = game:GetService("TweenService")
local Sounds = ReplicatedStorage.Sounds
local SpeedBoostStart = Sounds.SpeedBoostStart
local SpeedBoostEnd = Sounds.SpeedBoostEnd

ReplicatedStorage.Remotes.CardPick.OnServerEvent:Connect(function(player : Player, card : TextButton):()
	local spawnedcards = player.PlayerCardSpawn
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")::Humanoid
	local humanoidrootpart = character:WaitForChild("HumanoidRootPart")::Part

	if abilityname.Value == "QuickFeet" and humanoid.Health ~= 0 then

		print(card.Name .. " ability activated")
		local startsound = Instance.fromExisting(SpeedBoostStart)
		local endsound = Instance.fromExisting(SpeedBoostEnd)
		startsound.Parent = humanoidrootpart
		endsound.Parent = humanoidrootpart

		humanoid.WalkSpeed += 13
	end
end)

Here are some things I want to talk about regarding your scripting:

Indexing inside tables more than once is very expensive; you have to cache even for 2 lookups!
With instances it is even more expensive, so caching is mandatory.

Please name services exactly as they are. You are going to be very confused coming back to your code in 4 weeks, freaking out.
For that you may use plugins such as:
https://create.roblox.com/store/asset/11380642657
To autocomplete and insert service variables (I personally use this plugin)

“Character” inside a humanoid is a property! You have been trying to call :WaitForChild on that even though it’s not possible literally.

Use Instance.fromExisting over :Clone if the instance has no descendants because Instance.fromExisting is slightly faster; nonetheless, creating an instance from scratch will be a little bit faster than using :Clone or Instance.fromExisting.

Explaining how local character = player.Character or player.CharacterAdded:Wait() and “or” work:
It tries to index a player, but if it does not find one (not a truthy value nil/false/nullptr(void)), it uses a second argument, which triggers an event that should return you a character upon completion.
Although I would advise against using this approach here anyway since it may create an infinite thread if the player leaves during it.

3 Likes

the first event is when they pick the card and the 2nd event is for when they activate it

You have to move second event outside then.

1 Like

how do i get the right player to only activate it for them?

You should be checking it on server anyway.

I dont understand why are you structuring system in such odd way

I assume abilityname is parented inside player.

local uis = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tweenservice = game:GetService("TweenService")
local Sounds = ReplicatedStorage.Sounds
local Remotes = ReplicatedStorage.Remotes
local SpeedBoostStart = Sounds.SpeedBoostStart
local SpeedBoostEnd = Sounds.SpeedBoostEnd

Remotes.CardPick.OnServerEvent:Connect(function(player : Player, card : TextButton):()
	local spawnedcards = player.PlayerCardSpawn
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")::Humanoid
	local humanoidrootpart = character:WaitForChild("HumanoidRootPart")::Part

	
end)
Remotes.CardActivated.OnServerEvent:Connect(function(player : Player, card : TextButton):()
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")::Humanoid
	local humanoidrootpart = character:WaitForChild("HumanoidRootPart")::Part
	local abilityname = player.abilityname::StringValue

	if abilityname.Value == "QuickFeet" and humanoid.Health ~= 0 then

		print(card.Name .. " ability activated")
		local startsound = Instance.fromExisting(SpeedBoostStart)
		local endsound = Instance.fromExisting(SpeedBoostEnd)
		startsound.Parent = humanoidrootpart
		endsound.Parent = humanoidrootpart

		humanoid.WalkSpeed += 13
	end
end)
1 Like

i only rlly taught myself how to code, ig i just suck at programming..

No, you are not.
You just jumped more than you can handle YET*

I recommend you lock in on watching tutorials and on reading documentation and temporarily pause any project you are working on.

You can watch more DevForum tutorials and do your own experiments on the engine and language.
I recommend learning how threads (coroutines) actually work.
Difference between task and coroutine library, etc.

You can learn typecheck as well; it may be helpful at organizing stuff: Type checking - Luau

I have my own tutorial about typechecking as well if you are interested:

1 Like

This script you have:


local uis = game:GetService("UserInputService")
local replicated = game:GetService("ReplicatedStorage")
local tweenservice = game:GetService("TweenService")

game:GetService("ReplicatedStorage").Remotes.CardPick.OnServerEvent:Connect(function(player : Player, card : TextButton)
	local spawnedcards = player.PlayerCardSpawn
	local character = player.Character or player:WaitForChild("Character")
	local humanoid = character:FindFirstChild("Humanoid")
	local humanoidrootpart = character:FindFirstChild("HumanoidRootPart")

	game:GetService("ReplicatedStorage").Remotes.CardActivated.OnServerEvent:Connect(function()
		if abilityname.Value == "QuickFeet" and humanoid.Health > 0 then

			print(card.Name .. " ability activated")
			local startsound = replicated.Sounds.SpeedBoostStart:Clone()
			local endsound = replicated.Sounds.SpeedBoostEnd:Clone()
			startsound.Parent = character:FindFirstChild("HumanoidRootPart")
			endsound.Parent = character:FindFirstChild("HumanoidRootPart")

			humanoid.WalkSpeed += 13
		end
	end)
end)

It just has a bit of some strategizing issue…

This is your script’s problem:

the CardPick.OnserverEvent, when it just does it function, you made the local variable there.
anddd you had a remote event in there, that when that ability remote event gets fired.. It does ur quickfeet thing or whatever, the local variables u had were connected to that even, sooo that means whenever that remote event is fired, all players that got connected to the CardPick.Onserver event will get affected because u also had your card activated event in there, having to use your local variables in that cardpick event to do its “Thing”! Get it?

This will work if u just add a bit of lines (less than 5 holy)

I dont see a variable “abilityname” which is off ig.. well, lets just quite miss that, onto the next thing,
well I dont want to do any yapping so ill do the script for you
just a side note: you gotta check if that player is that player that got card picked lol

local uis = game:GetService(“UserInputService”)
local replicated = game:GetService(“ReplicatedStorage”)
local tweenservice = game:GetService(“TweenService”)

game:GetService(“ReplicatedStorage”).Remotes.CardPick.OnServerEvent:Connect(function(player : Player, card : TextButton)
local playerMe = player
local spawnedcards = player.PlayerCardSpawn
local character = player.Character or player:WaitForChild(“Character”)
local humanoid = character:FindFirstChild(“Humanoid”)
local humanoidrootpart = character:FindFirstChild(“HumanoidRootPart”)

game:GetService("ReplicatedStorage").Remotes.CardActivated.OnServerEvent:Connect(function()
	if abilityname.Value == "QuickFeet" and player.UserId == playerMe.UserId and humanoid.Health > 0 then

		print(card.Name .. " ability activated")
		local startsound = replicated.Sounds.SpeedBoostStart:Clone()
		local endsound = replicated.Sounds.SpeedBoostEnd:Clone()
		startsound.Parent = character:FindFirstChild("HumanoidRootPart")
		endsound.Parent = character:FindFirstChild("HumanoidRootPart")

		humanoid.WalkSpeed += 13
	end
end)

end)

there is still some problems you gotta fix, and I am too lazy to list that, figure it on your own? Right makes u learn

2 Likes

I couldnt do the right format holy

1 Like

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