Discuss Scratch

historical_supa
Scratcher
1000+ posts

Roblox scripting help

Dunno if this is supposed to be here, but it probably is.

Anyway, I'm making a Bloxy Cola healing drink thing in Roblox, and I used this old tutorial but I edited the code slightly. Something definitely went wrong. I tried to fix it by using different functions but nothing still works.

local CanActivate = true
local player = game.Players.LocalPlayer
local handle = script.Parent
local sound = handle:WaitForChild('Sound')

function onActivated()
if CanActivate == true then
CanActivate = false
wait(0.5)
player.Character.humanoid.Health = player.Character.humanoid.Health + 15
wait(0.5)
soundremove this)Play()
wait(7.5)
CanActivate = true
end
end

Could someone help me and make it so that the player heals whenever the item is used? (with the cooldown)

Also, other thing, there are two teams in my game, and every other player they just get into the winners team by joining. This is the code I'm attempting to use to stop that, sorry if I made a dumb silly mistake that's plain to see.

local plr = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function(player)
if plr.Team == game:GetService(“Teams”).Winners then
plr.Team = game:GetService(“Teams”).Playing
end
end)

It doesn't work in the workspace nor the severscriptservice thing. How should I fix this?

I'm a beginner to Lua and Roblox coding so please understand if I made obvious mistakes.

Last edited by historical_supa (Sept. 8, 2022 08:41:45)

k0d3rrr
Scratcher
1000+ posts

Roblox scripting help

Yo! Historical's back!
Well, to quote @pkhead:

pkhead wrote:

(8)
you should use task.wait instead of wait, as wait is now deprecated.
historical_supa
Scratcher
1000+ posts

Roblox scripting help

k0d3rrr wrote:

Yo! Historical's back!
Well, to quote @pkhead:

pkhead wrote:

(8)
you should use task.wait instead of wait, as wait is now deprecated.
It still doesn't work. I've changed nothing but that.
pkhead
Scratcher
1000+ posts

Roblox scripting help

I think the problem stems from this line:
player.Character.humanoid.Health = player.Character.humanoid.Health + 15
replace “humanoid” with “Humanoid” (the case of your identifiers matter in programming)

Also, what is
sound:(remove this)Play()
I don’t know if you actually put that in your script
Try replacing that with
sound:Play()

Also, using deprecated functions/methods isn’t usually the cause of error, as deprecation just means it’s an outdated practice, and may be removed at any time (although in most cases they don’t to preserve backwards compatibility)

One last thing, it’d be very helpful if you include error messages when you ask for scripting help.
historical_supa
Scratcher
1000+ posts

Roblox scripting help

pkhead wrote:

snip
I tried that. Still didn't work.

And yes, I didn't add the remove this thing, it's just to stop the emoji from getting in.

Also, there were no error messages at all.
pkhead
Scratcher
1000+ posts

Roblox scripting help

historical_supa wrote:

pkhead wrote:

snip
I tried that. Still didn't work.

And yes, I didn't add the remove this thing, it's just to stop the emoji from getting in.

Also, there were no error messages at all.
did you write
local tool = handle.Parent
tool.Activated:Connect(onActivated)
historical_supa
Scratcher
1000+ posts

Roblox scripting help

pkhead wrote:

historical_supa wrote:

pkhead wrote:

snip
I tried that. Still didn't work.

And yes, I didn't add the remove this thing, it's just to stop the emoji from getting in.

Also, there were no error messages at all.
did you write
local tool = handle.Parent
tool.Activated:Connect(onActivated)
Oh, that must be the part wherre I got it wrong. Huge thanks

edit: ok bruh nvm still doesn't work, don't know where i got it wrong

Last edited by historical_supa (Sept. 10, 2022 05:52:49)

pkhead
Scratcher
1000+ posts

Roblox scripting help

historical_supa wrote:

pkhead wrote:

historical_supa wrote:

pkhead wrote:

snip
I tried that. Still didn't work.

And yes, I didn't add the remove this thing, it's just to stop the emoji from getting in.

Also, there were no error messages at all.
did you write
local tool = handle.Parent
tool.Activated:Connect(onActivated)
Oh, that must be the part wherre I got it wrong. Huge thanks

edit: ok bruh nvm still doesn't work, don't know where i got it wrong
hmm
what type of script is it? is it a local script? because in normal scripts, which run on the server, you cannot access Players.LocalPlayer, as servers do not have a local player.

if it is a server script, instead of using player.Character, you should use tool.Parent, because when equipped, tools get parented to the player's character. if it is a local script, change it to a server script because humanoid.Health needs to be changed on the server.

and for the teams thing, instead of using a script, you can fix it by turning off the “AutoAssignable” property for the Winners team.

Last edited by pkhead (Sept. 10, 2022 15:04:59)

historical_supa
Scratcher
1000+ posts

Roblox scripting help

pkhead wrote:

historical_supa wrote:

pkhead wrote:

historical_supa wrote:

pkhead wrote:

snip
I tried that. Still didn't work.

And yes, I didn't add the remove this thing, it's just to stop the emoji from getting in.

Also, there were no error messages at all.
did you write
local tool = handle.Parent
tool.Activated:Connect(onActivated)
Oh, that must be the part wherre I got it wrong. Huge thanks

edit: ok bruh nvm still doesn't work, don't know where i got it wrong
hmm
what type of script is it? is it a local script? because in normal scripts, which run on the server, you cannot access Players.LocalPlayer, as servers do not have a local player.

if it is a server script, instead of using player.Character, you should use tool.Parent, because when equipped, tools get parented to the player's character. if it is a local script, change it to a server script because humanoid.Health needs to be changed on the server.

and for the teams thing, instead of using a script, you can fix it by turning off the “AutoAssignable” property for the Winners team.
For whatever reason, I don't know why it still doesn't work. it's a normal script that's a child of the handle, here's the code just in case

local CanActivate = true
local handle = script.Parent
local tool = handle.Parent
local sound = handle:WaitForChild('Sound')

function onActivation()
if CanActivate == true then
CanActivate = false
task.wait(0.5)
tool.Parent.Humanoid.Health = tool.Parent.Humanoid.Health + 15
task.wait(0.5)
soundlay()
task.wait(7.5)
CanActivate = true
end
end

tool.Activated:Connect(onActivation)

At least the teams were fixed.
historical_supa
Scratcher
1000+ posts

Roblox scripting help

bump
pkhead
Scratcher
1000+ posts

Roblox scripting help

if there are no errors, try printing something out whenever the onActivated function runs. this will confirm whether or not the activation is being registered. additionally, print something out when CanActivate is true. (alternatively you could use breakpoints which pause code execution on certain lines, and lets you go through code step by step. to use breakpoints you click on/next to the line number.)

and a child “Sound” does exist in the handle, right? and Handle is a direct child of the tool?

i remember in a game i was making, the activated event wouldn’t register in a tool i was making. eventually i figured out that it was because of a context action binding to the left mouse button in an entirely unrelated script, which would sink the mouse input preventing it from registering the tool activation. if this is the problem in your situation, you can try fixing it by making the context action function return Enum.ContextActionResult.Pass

Last edited by pkhead (Sept. 14, 2022 12:54:16)

historical_supa
Scratcher
1000+ posts

Roblox scripting help

pkhead wrote:

if there are no errors, try printing something out whenever the onActivated function runs. this will confirm whether or not the activation is being registered. additionally, print something out when CanActivate is true. (alternatively you could use breakpoints which pause code execution on certain lines, and lets you go through code step by step. to use breakpoints you click on/next to the line number.)

and a child “Sound” does exist in the handle, right? and Handle is a direct child of the tool?

i remember in a game i was making, the activated event wouldn’t register in a tool i was making. eventually i figured out that it was because of a context action binding to the left mouse button in an entirely unrelated script, which would sink the mouse input preventing it from registering the tool activation. if this is the problem in your situation, you can try fixing it by making the context action function return Enum.ContextActionResult.Pass
I used prints but only the first one appears in the output when I click it, which is a bit strange, since the first one is activated after all the local variables are set. There are two others that appear in the onActivation function but when I click it nothing happens. Using the return Enum thing in the code doesn't work either
pkhead
Scratcher
1000+ posts

Roblox scripting help

I tested your code in Studio and it works exactly as intended. Strange.

I think the only way I could help you further is if you invite me as a team create collaborator to your place, since I'm pretty sure something unrelated is what's causing your code to not function correctly. My username's Bisoph.

Powered by DjangoBB