I have tried to make a button that will give a player speed. However, I am not very good at scripting and tried some really stupid things, like: game.Workspace:FindFirstChild('Humanoid') which of course didn't work. It's for my upcoming game Universal Creative Centre. I have successfully made a script which turns on a ParticleEmitter and changes a Player Size, but I can't think of how to do the speed button thing.
Well it is very simple!
01 | local Player = game.Players.LocalPlayer -- getting the player |
02 | local Character = Player.Character -- getting the players' character |
03 |
04 | local Button = script.Parent -- where the button is located |
05 |
06 | Button.MouseButton 1 Click:Connect( function ) -- When button is press |
07 | if Player and Character then -- check if player is alive and exist |
08 | -- If alive exist then |
09 | Character.Humanoid.WalkSpeed = 20 -- Change "20" to the speed you want default 16 |
10 | end |
11 | end ) |
Hope it helps!
The question changed soooo:
1 | local Part = script.Parent |
2 | local CD = Part.ClickDetector -- CD == ClickDetector |
3 |
4 | CD.MouseClick:Connect( function (Player)) |
5 | local Character = Player.Character |
6 | if Character then |
7 | Character.Humanoid.WalkSpeed = 20 -- Change "20" to the speed you want default 16 |
8 | end |
9 | end ) |
If you want a GUI button, you need to set up some RemoteEvents first, so in ReplicatedStorage you'll put a RemoteEvent called "ChangeSpeedEvent".
Insert a local script inside of the text button and write this in it:
01 | local button = script.Parent |
02 | local players = game:GetService( "Players" ) |
03 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 |
05 | button.MouseButton 1 Click:Connect( function () |
06 |
07 | local player = players.LocalPlayer |
08 | local changeSpeedEvent = replicatedStorage:WaitForChild( "ChangeSpeedEvent" ) |
09 |
10 | changeSpeedEvent:FireServer() |
11 |
12 |
13 | end ) |
and a server script inside ServerScriptService with this in it:
01 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local changeSpeedEvent = replicatedStorage:WaitForChild( "ChangeSpeedEvent" ) |
03 |
04 | local function onChangeSpeedEvent(player) |
05 |
06 | local players = game:GetService( "Players" ) |
07 |
08 | local char = player.Character or player.CharacterAdded:Wait() |
09 |
10 | char:WaitForChild( "Humanoid" ).WalkSpeed = 2 |
11 |
12 | end |
13 |
14 | changeSpeedEvent.OnServerEvent:Connect(onChangeSpeedEvent) |
15 |
16 | end ) |
What that does is listen to when the player clicks a text button from a local script, then it fires the event that you set up previously. The server script listens to when that event has been fired and when it is it gets the character from the player who fired it (who clicked the button) and it sets the WalkSpeed to the number.
If you want a button like a part that you click on, insert a click detector and a server script inside the part and write this inside the script:
01 | local part = script.Parent |
02 | local clickdetector = part:waitForChild( "ClickDetector" ) |
03 |
04 | clickdetector.MouseClick:Connect( function (p) |
05 |
06 | local char = p.Character or p.CharacterAdded:wait() |
07 |
08 | char:WaitForChild( "Humanoid" ).WalkSpeed = 6 |
09 |
10 | end ) |
What that does is it gets the player from argument p
and gets the character and sets WalkSpeed in Humanoid to a number you want.
Hope we could help you.
1 | part.Touched:Connect( function (h) |
2 | local hum = h.Parent:FindFirstChild( "Humanoid" ) |
3 | if hum then |
4 | hum.WalkSpeed = 25 -- change it to whatever you like! |
5 | end |
6 | end ) |
Hello i've used @XviperIink 's code cause then i don't need to remake the whole client sided script:
simple version:
LocalScript >
1 | local Button = script.Parent -- where the button is located |
2 |
3 | Button.MouseButton 1 Click:Connect( function ) -- When button is press |
4 | local speed = 20 -- u can change this to anything. |
5 | game.ReplicatedStorage:WaitForChild( "RemoteEvent" ):Fire(speed) |
6 | end ) |
ServerScript >
1 | local remote = Instance.new( "RemoteEvent" ,game.ReplicatedStorage) |
2 |
3 | remote.OnServerEvent:Connect( function (player,speed) |
4 | local player = workspace:FindFirstChild(player.Name) |
5 | if player then |
6 | player.Humanoid.WalkSpeed = speed |
7 | end |
8 | end ) |